Link to home
Start Free TrialLog in
Avatar of gothic130
gothic130

asked on

How many times a letter is in a string...

I'm doing a program to know how many times a letter is in a string. Any idea??? :) Thanks!
I got this but  I think I'm lost:

#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
 char *string;
 int long, n;
 clrscr();
 gets(&string[0]);
 long=strlen(string);
 for(n=long; n>=0; n--)
 {
   printf("%s\n",string);
 }
 getch();
}
Avatar of n_fortynine
n_fortynine

I doubt that you can name your variable "long" since it's a reserved keyword for the type with that same name.
What you should do is basically traverse the string (you can use the loop there) and use an int counter to denote how many times you've run into the desired letter. Start out at 0 and increment this counter each time you see it. Works for you?
Avatar of gothic130

ASKER

Could you explain it deeply? I don't get quite right :( (I'm a begginer)
I missed the part that you cannot call your string "string" for the same reason I said.
Well, since you have found the length of you string, you can use the for loop you have written there, except that instead of  printf("%s\n",string); you do:
loop from the last index to the first index (be careful with these indexes)
  compare the i-th element of the string to the desired letter, if they're equal you add 1 to the value of the counter.
Simple as that :)
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  compare the i-th element of the string to the desired letter, if they're equal you add 1 to the value of the counter.
This part is what should be in place of printf(...) I hope I didn't confuse you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Short, sweet, and simple.

And the whole homework done :o)
WOW but Kent, the code you wrote can be done without using stdlib.h ???
Maybe I explained wrong what I want, but I want the program to ask for a string and then it's suppose to give the letters that repeats the most (without specify any letter). i.e. String: hello
Letter l  repeats 2 times.

String: Hello there
Letter e repeats 3 times.
>>Maybe I explained wrong what I want, but I want the program to ask for a string and then it's suppose to give the l>>etters that repeats the most (without specify any letter). i.e. String: hello
>>Letter l  repeats 2 times.

Misunderstood? At least I did :)

>>I'm doing a program to know how many times a letter is in a string
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above program is case sensitive....
if you want to make it case insensitve then

change int num = caCount[ caBuffer[i] ]++ to

 int num = caCount[ tolower(caBuffer[i]) ]++

 Dennis
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THIS program does exactly what you want..

#include <stdio.h>
main()
{
 char string_buf[1024],*temp;
 char character_to_search;
 int  count = 0;
 
// READ THE STRING
fflush(stdin); //flush the std in stream
puts("ENTER THE STRING : ");fgets(string_buf,1024,stdin);
// REad the character to search
puts("ENTER THE STRING : ");scanf("%c",&character_to_search);
//NOW THE STRING IS THERE IN THE string_buffer and
//String is a character array terminated by a null character
//we can search char by char.
temp = string_buf;// point the temp pointer to beginning of the string

while(temp !='\0')/*a string terminates with a  '\0' or null char*/
{
    if(*temp == character_to_search) count ++;
    temp++; // move on to next character
}
//print the output  
printf("THE STRING '%s' has the character '%c'   %d  number of times ",string_buf,character_to_search,count);
}
Dennis, could you explain me why did you put this: {0} in this line: int caCount[256] ={0};  and why the number has to be such big (256)???
He has initialized the whole array [all array locations] to zero

And regarding 256, its because of the ascii values of the characters u enter

suppose u enter aab

Then the array would be populated as shown below

caCount[97] = 2
caCount[98] = 1

Rest all the locations of the caCount array would contain 0.

This is because the ascii value of a is 97, that of b is 98
>>caCount[256] ={0};

Is there any otherway to initialize the array?

int caCount[256] =
{
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};


or within the program, do:

 memset (caCount, 0, sizeof (caCount));



Kent
Thanks to all of you.