I'm trying to show which character and how many of each of them that are in an sentence that the user choose. So if the user puts in "Hello World!" the program should give back one char and the number of times it is used.
" SPACE:1, !:1, H:1, W:1, e:1, d:1, l:3, o:2, r:1,"
I'm having it in an switch because I have other choices that the user can choose between.
Right now I can get out what char that is used and how many of them from SPACE to Q. I also can get out all the small letters but if it reads an 'a' it will say that there is 1 'a' and one SPACE (in ASCII code it start over from 32 and goes up as the small letters goes up).
These are the variables that I use.
int menyval = 0, i, k = 0, h, j, count, count2;
char input, str[100], getridof, add, character;
Here is what I have in this case.
printf("Write a string not more then 50 chars:\n");
getchar();
i = 0;
j = 0;
count = 0;
int counts[50] = { 0 };
gets(str);
str[j] = str[i];
while (str[i] != '\0') {
if (str[i] >= 97 && str[i] <= 122) {
counts[str[i] - 97]++;
}
i++;
count++;
}
for (i = 0; i < 50; i++) {
if (counts[i] != 0) {
printf("%c: %d\n", i + 97, counts[i]);
}
}
while (str[j] != '\0') {
if (((str[j] >= 32 && str[j] <=96)) || ((str[j] >=123 && str[j] <= 126))) {
counts[str[j] - 32]++;
}
j++;
}
for (j = 0; j < 50; j++) {
if (counts[j] != 0 ) {
//if((j) < 127)
printf("%c: %d\n", j + 32, counts[j]);
}
}
printf("Total amount of char: %d\n", count);
str[i] = '\0';
system("pause");
system("cls");
This is a school assignment, so I understand if you don't want to say the direct code but I'll be very thankfull for some hints to point me in the right direction.