Write a program that can count the number of the alphabet from the string.
Example string " Complicated Question"
I need a index.
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]++; }
long time no C; //seige
You miss 100% of the people you don't C;
Code:if (language != LANG_C && language != LANG_CPP) drown(language);
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
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.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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]); } }