need help on finding char in string [newbie]

This is a discussion on need help on finding char in string [newbie] within the C++ Programming forums, part of the General Programming Boards category; hi everyone, can someone make this code easier? Code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    need help on finding char in string [newbie]

    hi everyone, can someone make this code easier?

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int main() 
    {
        int numOfChar[26];
        int sum = 0;
        string lowerCase;
        string name;
      
        lowerCase = "", name = "";
        
        // initialise array of string to 0.
        for (int i = 0; i < 26; i++)
        
           numOfChar[i] = 0; 
        
        // Prompt user input    
        cout << "name please: ";
        cin >>name;    
    
        // convert to lower case
    
        for (int i = 0; i < name.length(); i++)
        
            lowerCase += tolower(name[i]); 
            
        //
    
        for (int i = 0; i < lowerCase.length(); i++) 
        {
          if (lowerCase[i] - 'a' < 26 && numOfChar[lowerCase[i] - 'a'] == 0) 
          {
            numOfChar[lowerCase[i] - 'a'] = 1;
          }
        }
        
        //
    
        for (int i = 0; i < 26; i++)
          sum += numOfChar[i];
    
       cout << "\nYou have " << sum << " different letters in your string.\n" 
            << endl;
            
       system("pause");
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    can some one explain these codes

    Code:
     lowerCase = "", name = "";
        
        // initialise array of string to 0.
        for (int i = 0; i < 26; i++)
        
           numOfChar[i] = 0;
    Code:
    lowerCase += tolower(name[i]);
    Code:
        for (int i = 0; i < lowerCase.length(); i++) 
        {
          if (lowerCase[i] - 'a' < 26 && numOfChar[lowerCase[i] - 'a'] == 0) 
          {
            numOfChar[lowerCase[i] - 'a'] = 1;
          }
        }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    tolower() returns a character converted to lowercase. So if its argument is 'B', it will return 'b'. It returns 'h' unchanged.

    Code:
     lowerCase = "", name = "";
        
        // initialise array of string to 0.
        for (int i = 0; i < 26; i++)
        
           numOfChar[i] = 0;
    This code initializes everything. It sets lowerCase and name to empty strings, and every element in the array numOfChar to zero.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    hello, isnt this code

    Code:
    lowerCase += tolower(name[i]);
    means lowerCase = lowerCase + tolower(name[i]); ?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, it is. I thought you knew what that meant.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    but when i use this
    Code:
    lowerCase = lowerCase + tolower(name[i]);
    the program didnt work ..
    what is the problem please..

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    lowerCase += tolower(name[i]);
    is exactly the same as
    Code:
    lowerCase = lowerCase + tolower(name[i]);
    Try changing it again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Code:
     for (int i = 0; i < name.length(); i++)
        
            lowerCase += tolower(name[i]);
    i think your missing the braces { }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You don't need braces. If they're not there, the for loop operates on one statement, the following one.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 01:22 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21