470,571 Members | 2,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Please help me>>>>

how to wirte C program to calculate and print the number of different symbols used in a given string. A blank space counts as one symbol.

Input: Any string. The length is limited to 60 characters. For example:

Enter a string: Mississipi

Test data:
#1. “Say good bye!”
#2. “The world is round”

Output: The number of different symbols. For example, the output for the string “Mississipi” is

Number of symbols for the string “Mississippi” is: 4
can you help me....
Oct 19 '08 #1
2 1020
Declare one array of charachters for storing unique symbols and called symbols[100], and declare one integer n for storing the number of symbols and set it intially to zero, and an integer called test to check if a symbol in the string is repeated, and ofcourse declare an array string[100] to store the string.
Now start with this for loop: for(int i=0 ; i<strlen(string) ; i++) so you can check every symbol in the string, then set the integer test to 0 inside this for loop, then also inside this for loop use another for loop which is: for(int j=0 ; j<n ; j++) to check if this symbol is repeated by using the array symbols[] where j is its index(symbols[j]), and if this symbol(which is string[i]) is found in the array symbols[] then set the integer test to 1 and break from the second loop, and if it is not found set it to 0. Now after the second for loop is finished(either way) check the integer test, if it is equal to 0 put this symbol(which is symbol[i]) in the the array symbols(i.e: write symbols[n] = string[i]) and then increment n( meaning n++ ). But if test is equal to 1 do nothing(meaning the charachter string[i] is present more than once in the string), and here the first for loop is repeated.
And now you have the number of symbols, which is n.
Oct 19 '08 #2
thanks very much..........................
Oct 20 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by aze | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.