monitoring
+ Reply to Thread
Results 1 to 7 of 7

Thread: C++ Help Needed

  1. #1
    Join Date
    Sep 2006
    Posts
    1,649

    C++ Help Needed

    I am making a simple program that takes a sentence(s) and counts up all of the letters in the given sentence(s). Now, I was wondering if anyone could write some source code because of my problem

    I want an easier way to write a way to get letters besides using a huge if-then-else loop, such as "If char X == a or A, then add * to variable L, else if char X == b or B then..." so on and so forth (Yes, i didn't write that using C++ coding, just giving you the idea of what I would otherwise have to do)

    If anyone could help, it would be appreciated

  2. #2
    Join Date
    Sep 2006
    Posts
    32
    Quote Originally Posted by Moonbat
    I am making a simple program that takes a sentence(s) and counts up all of the letters in the given sentence(s). Now, I was wondering if anyone could write some source code because of my problem

    I want an easier way to write a way to get letters besides using a huge if-then-else loop, such as "If char X == a or A, then add * to variable L, else if char X == b or B then..." so on and so forth (Yes, i didn't write that using C++ coding, just giving you the idea of what I would otherwise have to do)
    Try this:
    Code:
    char sentence[256];
    cin.getline(sentence,256);
    int length=strlen(sentence);
    cout<<length;
    The function strlen() is used to count the amount of characters in a string. This code will print the amount of characters in the sentence. Hopefully this will help.

  3. #3
    Join Date
    Sep 2005
    Posts
    2,050
    Whenever manipulating strings in c++, you should check this reference:

    [url]http://www.cplusplus.com/ref/cstring/[/url]

    strcat(), strcpy(), strcmp(), and strlen() are vital components of all c++ programs, so learn how to use them and you'll find things a lot easier than trying to re-invent the wheel.

  4. #4
    Join Date
    Sep 2006
    Posts
    1,649

    but

    I know about strlen, but I want only to get letters, not spaces or punctuation. Is there any easier way to get only letters?

  5. #5
    Join Date
    Sep 2006
    Posts
    32
    Quote Originally Posted by Moonbat
    I know about strlen, but I want only to get letters, not spaces or punctuation. Is there any easier way to get only letters?

    Code:
    char i[256];
    cin.getline(i,256);
    int len=strlen(i);
    for(int m=0;m<256;m++){
    if(i[m]==" " || i[m]=="." || i[m]=="?" || i[m]=="!" || i[m]==","){
    len--;
    }else{
    continue;
    }
    
    }
    cout<<len;
    This is one way to do it, though there might be a better way.
    Last edited by tocksarcle; 10-01-2006 at 12:16 AM.

  6. #6
    Join Date
    Sep 2006
    Posts
    1,649

    well

    It worked just fine for me.
    'Preciate it tocksarcle

  7. #7
    Join Date
    Sep 2006
    Posts
    32
    Quote Originally Posted by Moonbat
    It worked just fine for me.
    'Preciate it tocksarcle
    No problem

+ Reply to Thread

Similar Threads

  1. Help Needed !!
    By patrickl841 in forum General discussion
    Replies: 6
    Last Post: 08-26-2011, 09:22 AM
  2. just what i needed
    By xztheericzx in forum Programming
    Replies: 10
    Last Post: 11-06-2007, 07:13 PM
  3. Help needed.
    By Water-Dragon in forum Internet Privacy
    Replies: 23
    Last Post: 12-11-2006, 12:24 AM
  4. Help Needed!!!
    By Alucard in forum Internet Privacy
    Replies: 1
    Last Post: 04-19-2006, 02:42 PM
  5. help needed
    By AK03 in forum Internet Privacy
    Replies: 3
    Last Post: 02-06-2006, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts