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!

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
main() {
char sentence[35];
char *letter[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w" "x", "y", "z"};
int num[26] = {0};

     printf("\nEnter a sentence or phrase\n");
     scanf("%s", &sentence);

 // loop to find alphabet characters and count each
    for(int j=1; j<=26; j++) {

     if(strchr(sentence, *letter[j]) != NULL) {
               ++num[*letter[j]];
 
printf("\'%c\' was found in \"%s\" %d times. \n",*letter[j], sentence, num[j]);
 }
}
 
}

Recommended Answers

All 8 Replies

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.

CODE tags, as the intro thread says, which you've obviously missed on your stampede to get your question asked as soon as possible.

Salem what are you talkin bout that doesn't matter its just code???Gosh

>Salem what are you talkin bout that doesn't matter its just code???
That's why you're supposed to use them. :icon_rolleyes:

> 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.

commented: Good in a good tone +7
commented: (Y) +3
commented: I must be tone hearing impaired. I don't hear here any tone. +1

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!

>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.

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:

char *letter[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w" "x", "y", "z"};
[...]
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.


[edit]
Don't take Narue's post to serious, she has a.. euhm.. 'special' way of putting things :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.