Write a program that can count the number of the alphabet from the string.
Example string " Complicated Question"
I need a index.
Printable View
Write a program that can count the number of the alphabet from the string.
Example string " Complicated Question"
I need a index.
Took < 5mins. Something like that anyway. Now you have to modify that to use pointers, to improve. BTW, the number of times each character occurs is stored in alpha (u can figure out how to access each element.Code:char string[50] = " Complicated Question";
string = tolower((unsigned char)string);
int alpha[26];
int i;
for (i=0; i<50; i++) {
alpha[string[i]-97]++;
}
P4R4N01D: What is 32 - 97, and where in your 26 element array is that? Perhaps "isalpha" would be useful inside your loop?
Perhaps 50 should be replaced with the length of the string?Code:for (i=0; i<50; i++) {
--
Mats
Also, tolower() takes a single character (as an int), not a whole string.Code:string = tolower((unsigned char)string);
Took < 5 secs to find some bugs. :)
P4R4N01D has the right idea, it's all about ascii values. But you guys are getting bogged down with unnecessary bonus functions:
Code:#include <stdio.h>
int main(int argc, char *argv[]) {
short int cv, i=0, total=0;
while (argv[1][i] != 0) { // change to lowercase
if ((argv[1][i] > 64) && (argv[1][i] < 91)) argv[1][i]+=32;
if ((argv[1][i] < 97) || (argv[1][i] > 122)) {
i++; // skip non-letters
continue;
}
cv = argv[1][i]-96;
total += cv;
i++;
}
printf("%s = %d\n", argv[1],total);
}
gcc -o kabblah kabblah.c
./kabblah "Complicated Question"
complicated question = 221
The OP needed this converted to use pointers not array indexing. Also this issue is very similar to strlen() and there are numerous examples on the web on how to do just that. Is there any benefit to setting tolower when you only need to check the count of letters within the string?
Also working with the ACII table is very good when checking if it us upper case or not and doing the appropriate math to convert if necessary.
Was the below requirment changed? As I did not catch that....
"Write a program that can count the number of the alphabet from the string."
Ah, I see the problem. Lol, if the OP has the "C Programming" book, by any chance, this is covered pretty well, if I remember correctly, in order to check if a letter of the alphabet is found and thus increments the array element.
Actually, considering that I have met quite a few people who confuse "letter" and "alphabet", it could be the case that the OP is asking how to count the number of letters in a string.
This may not be exactly what the OP needed but though I would try to remember the solution from the book, just do not have it on hand but it went something like this:
Usage:
./countLetters "count my letters"
Code:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
int letters[26] = {0};
char ch = 'a';
while (*(argv[1]))
{
letters[(*(argv[1])++) - 'a']++;
}
printf ("Letters found are: \n");
for (i = 0 ; i < 26 ; i++)
{
printf ("%c = %d \n", ch++, letters[i]);
}
}