1. ## Strings

Hi there, iv recently got this question:

Write a fully modular C program that reads in a string of a defined size from the keyboard and reports how many times each letter of the alphabet occurs within that string (ignoring case). It should also report how many non-letters are included in the string.

I understand what it's asking me to do, i just can't get my head around a function that reports how many times each letter of the alphabet occurs/how many non letters occur within the string without using a giant switch-case statement.

Any help with regards to how i could report how many times each letter of the alphabet/ how many non letters occur within the entered string would be greatly appreciated. I'm not asking someone to do it all for me, just point me in the right direction .

Here's my code so far that reads in a string and converts it to lower case. I know it's not much.. but it's a start.

Code:
```
#include <stdio.h>
#include <string.h>

{

printf("Enter string: ");
scanf("%s", line);
strlwr(line);

return;
}

{

const int MAXSIZE = 100;
char line[MAXSIZE] = {'\0'};

return(0);

}```

2. Well, set yourself down with a pencil and paper...
Now work the problem as though you are the computer...
Write a letter ... add to a count for that letter
write another letter ... add to a count add to a count...
and so on.

Try some different approaches... see if you can figure out a real simple way of doing it.

Then when you think you finally understand the problem, write up some quicky point by point notes on what you need to code it.

Then... finally turn on your computer and start writing code.

One of the first lessons a programmer learns is that you simply can't code the solution to a problem you don't understand.

FWIW... You should be able to do this in about 25 lines of code...

3. Thanks, I'm doing that right now, i understand the problem... I'm just finding it hard to put the solution i have in my head into code. I'm very new to C programming, anyway thanks again.

4. Originally Posted by joel0z
Thanks, I'm doing that right now, i understand the problem... I'm just finding it hard to put the solution i have in my head into code. I'm very new to C programming, anyway thanks again.
Write it as "pseudo code"... sort of a pigeon english of your own making... then translate it to C...

On of the big problems people have when they first start coding is that they think in big blobs... "Just count all the As..." but the computer works in much smaller steps... set to start of string, what is first character? Where to count first character? How to increase the count?... now do next character... One of the diciplines is to learn to think in blobs at least as small as the computer does...

5. Thanks again, ill write up some pseudo code and post it on here, hopefully you can give me some help with that.

6. sigh.. i can't think of a way to do it without simply just iterating through the string using a massive switch-case statement to increment of the 27 integer variables(one for each of the letters of the alphabet and an additional variable to store the non-alphabetic characters). I know it would work... but it's poor design and i know there's a much more efficient way... I just can't figure out how to do it, surely it's not that hard. geez..

7. Looping through the string is the way to go, but you don't need a huge switch when there are built in library functions that will work for you. Look at the ctype.h functions.

Quzah.

8. thanks quzah I got the program to compile I'm pretty sure it does what the question asks. here's the code:

Code:
```#include <stdio.h>
#include <string.h>
#include <ctype.h>

{

printf("Enter string: ");
scanf("%s", line);
strlwr(line);

return;
}
void counter(char line[], int &count)
{

while(line[count])
{
if(isalpha(line[count]))
printf("Character %c is alphabetic\n",line[count]);
else
printf("character %c is not alphabetic\n",line[count]);
count++;
}

return;
}

int main()
{

const int MAXSIZE = 100;
char line[MAXSIZE] = {'\0'};
int count;

count = 0;

counter(line,count);

return(0);

}```

9. The fundamental flaw is using C++ compiler to compile C code.
Code:
`void counter(char line[], int &count)`
Edit:

Code:
```const int MAXSIZE = 100;
char line[MAXSIZE] = {'\0'};```
This will produce compiler error for C89 compiler.
If this compile without any error, it means you're using C99 Variable Length Array(VLA).

Code:
`#define MAXSIZE 100`
seems much more better choice since the size is known at compile time.

You could use if ignoring case,

Code:
```int alpha_count[26] = {0};
if( isalpha(line[i]) ) {
alpha_count[ tolower(line[i]) - 'a'];
}```
Pick a good book on C and learn. Don't confuse C and C++. and of course know what C standard feature you are using.

10. Originally Posted by joel0z
sigh.. i can't think of a way to do it without simply just iterating through the string using a massive switch-case statement to increment of the 27 integer variables(one for each of the letters of the alphabet and an additional variable to store the non-alphabetic characters). I know it would work... but it's poor design and i know there's a much more efficient way... I just can't figure out how to do it, surely it's not that hard. geez..
Your thinking process is on the right track.

Yes, pretty much 27 integer variables.
Whenever there’s a need for a lot of variables, yet each variable does similar jobs as the rest, think of using an array.

26 elements, one for each letter counter. Then access the array element that needs incrementing using an index which was calculated based on the letter. 'A' is 0, 'B' is 1, etc. There's a simple formula for that.

11. Ok... let me give you a hint where to start... Read the file or string character by character not line by line.