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();
}
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();
}
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 :)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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.
This part is what should be in place of printf(...) I hope I didn't confuse you.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Short, sweet, and simple.
And the whole homework done :o)
And the whole homework done :o)
ASKER
WOW but Kent, the code you wrote can be done without using stdlib.h ???
ASKER
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.
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
>>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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
if you want to make it case insensitve then
change int num = caCount[ caBuffer[i] ]++ to
int num = caCount[ tolower(caBuffer[i]) ]++
Dennis
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,s tdin);
// REad the character to search
puts("ENTER THE STRING : ");scanf("%c",&character_t o_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,cou nt);
}
#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,s
// REad the character to search
puts("ENTER THE STRING : ");scanf("%c",&character_t
//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_
}
ASKER
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
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
ASKER
>>caCount[256] ={0};
Is there any otherway to initialize the array?
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,
};
or within the program, do:
memset (caCount, 0, sizeof (caCount));
Kent
ASKER
Thanks to all of you.
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?