# Thread: Give an idea of finding different letters in the string

1. ## 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

2. Maybe I'm just tired but I'm really having trouble figuring out what you mean to do. Can you elaborate more.

3. i am sorry
here is an example

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

4. 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.

5. hi SlyMaelstrom , can you give me an example of doing this?

6. 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.

7. 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:```

8. 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. this is a bit confuse. anyone has a better way to do this question?

10. 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.

11. ok, thank you