Give an idea of finding different letters in the string

This is a discussion on Give an idea of finding different letters in the string within the C++ Programming forums, part of the General Programming Boards category; hi, can someone give some idea on how to find the different letters in the string. for example : Code: ...

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

    Smile Give an idea of finding different letters in the string

    hi, can someone give some idea on how to find the different letters in the string. for example :

    Code:
         for(int i = 0; name[i]; i++)
         {
        	lowerCase = tolower(name[i]);//convert to lowercase
        	
        	
         }
    for example, input = Johnny
    output will be = 5, as 'n' is repeated.

    firstly, i convert it to lower case so it is case insensitive. can i use some other C library that will make this simple and easy?

    would be really appreciate if someone show me an example. thank you
    Last edited by gtr_s15; 12-03-2005 at 03:08 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Maybe I'm just tired but I'm really having trouble figuring out what you mean to do. Can you elaborate more.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    i am sorry
    here is an example

    for example, input = Johnny
    output will be = 5, as 'n' is repeated.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Oh. Sure that's possible. A real simple way is to make a bool array with an index of 26. Initialize all them to false, then run through your array. Check the letter, go to it's correspondence index. If it's false make it true. Then after you do that, just run through your array again and add up all the trues.

    There also might be a STL function that does what you're asking, but I'll leave that one to the pros.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    hi SlyMaelstrom , can you give me an example of doing this?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    for (int i = 0; i < lowerCase.length(); i++) {
       if (lowerCase[i] - 'a' < 26 && alphabet[lowerCase[i] - 'a'] == 0) {
           alphabet[lowerCase[i]) - 'a'] = 1;
           }
    }
    
    for (int i = 0; i < 26; i++) {
        sum += alphabet[i];
    }
    Think along these lines. By the way you have an error in your first loop that would make your lowercase sting incorrect.
    Last edited by SlyMaelstrom; 12-03-2005 at 03:45 AM.
    Sent from my iPadŽ

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    If you don't quite understand that and are skeptical as to whether it works, here is the algorithm in use in the output of a small program I just made.

    Code:
    Write a string to see how many letters in the alphabet it uses:
    I love C++ programming!
    
    You have 12 different letters in your string.
    
    Write a string to see how many letters in the alphabet it uses:
    I love C programming!
    
    You have 12 different letters in your string.
    
    Write a string to see how many letters in the alphabet it uses:
    Hello World!
    
    You have 7 different letters in your string.
    
    Write a string to see how many letters in the alphabet it uses:
    Supercalifragilisticexpialidocious
    
    You have 15 different letters in your string.
    
    Write a string to see how many letters in the alphabet it uses:
    The quick brown fox jumps over the lazy dog!
    
    You have 26 different letters in your string.
    
    Write a string to see how many letters in the alphabet it uses:
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There also might be a STL function that does what you're asking, but I'll leave that one to the pros.
    Yes there is. The container: <set>, and the function: insert().

    You can use the STL container <set> to insert() all the chars in an entire string at one shot. Since a <set> will not allow you to insert() duplicates, the size() of the set(minus any spaces) will be the number of unique characters. The set will also sort the letters alphabetically.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    this is a bit confuse. anyone has a better way to do this question?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by gtr_s15
    this is a bit confuse. anyone has a better way to do this question?
    No... the way 7Stud showed and the way I showed are not only as easy as this process gets, they're both just about as easy as programming gets. Programming is not magic. You have to do some thinking to get things done. Just study my code or study the functions 7stud mentioned and you'll get this in no time.
    Last edited by SlyMaelstrom; 12-03-2005 at 05:02 AM.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    ok, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Arrange letters of the string alphabetically
    By ama_cuber in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 03:40 AM
  3. problems with string finding a char in da middleinitial!!
    By macedonio in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2007, 04:53 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM

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