Counting Alphabet Characters, Words, and String Length!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 3
Reputation: PeeJay is an unknown quantity at this point 
Solved Threads: 0
PeeJay PeeJay is offline Offline
Newbie Poster

Counting Alphabet Characters, Words, and String Length!

 
0
  #1
May 1st, 2007
I am suppose to create a program that counts the number of alphabets in string, words in a string and the lenght of the string and print them out! I have some code but I'm not sure how to get the string counted.

Can anyone please offer some help?


I pretty much have the basics I just need help counting the alphabets and finding out how to count the length and words that appear more than once!
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. main() {
  6. char sentence[35];
  7. char *letter[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
  8. "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w" "x", "y", "z"};
  9. int num[26] = {0};
  10.  
  11. printf("\nEnter a sentence or phrase\n");
  12. scanf("%s", &sentence);
  13.  
  14. // loop to find alphabet characters and count each
  15. for(int j=1; j<=26; j++) {
  16.  
  17. if(strchr(sentence, *letter[j]) != NULL) {
  18. ++num[*letter[j]];
  19.  
  20. printf("\'%c\' was found in \"%s\" %d times. \n",*letter[j], sentence, num[j]);
  21. }
  22. }
  23.  
  24. }
Last edited by WaltP; May 2nd, 2007 at 3:13 am. Reason: Please learn to use CODE tags -- read the announcements at the top of the forum
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #2
May 1st, 2007
Originally Posted by PeeJay
if(strchr(sentence, *letter[j]) != NULL) {
++num[*letter[j]];

printf("\'%c\' was found in \"%s\" %d times. \n",*letter[j], sentence, num[j]);
you are not using any counter to know how many letters you have. You should create a variable that will count how many times this process is true.
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #3
May 1st, 2007
[code][/code] tags, as the intro thread says, which you've obviously missed on your stampede to get your question asked as soon as possible.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: PeeJay is an unknown quantity at this point 
Solved Threads: 0
PeeJay PeeJay is offline Offline
Newbie Poster

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #4
May 1st, 2007
Salem what are you talkin bout that doesn't matter its just code???Gosh
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #5
May 1st, 2007
>Salem what are you talkin bout that doesn't matter its just code???
That's why you're supposed to use them.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Counting Alphabet Characters, Words, and String Length!

 
3
  #6
May 2nd, 2007
> Salem what are you talkin bout that doesn't matter its just code???Gosh
Because with correct indentation, I can figure out what code does about 10x quicker than a horrid unformatted mess like what you've posted.

So on seeing that most people who would help either
- drop your post to the bottom of their "to do" list, and if they run out of time, you lose.
- simply forget about trying to help you as you can't even be bothered with the simple niceties, so you lose again.

But rather than fixing the problem, you instead want to bitch about my attempt to get you to read the rules and improve your post quality.

Nah, forget it, you simply aren't worth the effort anymore.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: PeeJay is an unknown quantity at this point 
Solved Threads: 0
PeeJay PeeJay is offline Offline
Newbie Poster

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #7
May 2nd, 2007
Salem thanks for the advice. However, I would suggest you use better diction when talking to people. Your point would come across a little better and could be more effective if you didn't come across as such a"know it all" so to speak! I am new to this discussion community as you've probably already seen but I'm not a dummy so don't talk to me like one! Thanks again for the advice and watch the diction!
Last edited by PeeJay; May 2nd, 2007 at 11:42 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #8
May 2nd, 2007
>I am new to this discussion community as you've probably already seen
>but I'm not a dummy so don't talk to me like one!
Whether you're new or not is irrelevant. You've shown a lack of concern for proper formatting despite being told in detail the merits of it. I'm sorry, but that makes you look like a "dummy", and when you look like a dummy, you'll be treated like one.

>Your point would come across a little better and could be more effective
>if you didn't come across as such a"know it all" so to speak!
The message is there. If you don't care to see it and instead focus on imagined insults, that's not Salem's problem. Of course, even if you watch your diction, there will still be misunderstandings. For example, your little suggestion and choice of words strikes me as haughty. So maybe you should watch your diction.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Counting Alphabet Characters, Words, and String Length!

 
0
  #9
May 2nd, 2007
Originally Posted by PeeJay View Post
Salem thanks for the advice. However, I would suggest you use better diction when talking to people. Your point would come across a little better and could be more effective if you didn't come across as such a"know it all" so to speak! I am new to this discussion community as you've probably already seen but I'm not a dummy so don't talk to me like one! Thanks again for the advice and watch the diction!
We all know you're new, but you could have just read the instructions. With that said:
  1. char *letter[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
  2. "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w" "x", "y", "z"};
  3. [...]
  4. for(int j=1; j<=26; j++) {
This will give you a memory exception because you have 26 array-elements: 0-25 that is. So when you try to write to 26 the program will crash.

Regards Niek

[edit]
Don't take Narue's post to serious, she has a.. euhm.. 'special' way of putting things
Last edited by niek_e; May 2nd, 2007 at 11:55 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5016 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC