# Thread: Count number of letters...

1. ## Count number of letters...

I'm currently teaching myself C from a book and as one of the exercises it asks to write a program that counts the number of letters, digits, and common punctuation symbols entered by the user.

I have no idea how to do this and have been wrestling with this idea for a few hours now

not asking anyone to write the entire program for me, just a few pointers in the right direction to get me started would be great

supposed to use switch statement

2. If you have been wrestling with it, I'm going to make a leap of faith here and assume that you actually have some code or pseudo code. Slap it in here and we'll see where you're stuck.

Quzah.

3. Originally Posted by quzah
If you have been wrestling with it, I'm going to make a leap of faith here and assume that you actually have some code or pseudo code. Slap it in here and we'll see where you're stuck.

Quzah.
Don't have anything, dont know where to start

have tried to start it, but dont know what kind of loop or anything

4. Well, I would start by taking a look at the following:
All of which can be found by looking through the cctype header file

5. I assume you know how to check each character in the string, but do not know how to determine if the character is a number, letter or puntuation.

Have a look at 'ctype.h'

It contains functions to determine if a character (char variable) is a letter, number or punctuation (ie isdigit(), ispunct() etc)

6. Originally Posted by novacain
I assume you know how to check each character in the string, but do not know how to determine if the character is a number, letter or puntuation.

Have a look at 'ctype.h'

It contains functions to determine if a character (char variable) is a letter, number or punctuation (ie isdigit(), ispunct() etc)
Well I know about scanf, but i dont know how to reiterate if i have the user enter 10 letters or 50

7. Originally Posted by sponi
Don't have anything, dont know where to start

have tried to start it, but dont know what kind of loop or anything
Don't start with code. Start with a description of the problem. Think about the description, and split the job into tasks. Split each job into smaller steps until you can't any further. Now think about how to make each step a line (or two, whatever) of code.

Let's say I needed to move everything out of my closets and onto the floor. Let me put that into steps:

1. Go to a closet.
2. Open it.
3. Take something out.
4. Throw it on the floor.
5. Do that until the closet is empty.
6. Do that for each closet.

There, see how easy that was?
Code:
```for each closet
open closet
for each item in the closet
take item out of closet
throw item on floor```

Quzah.

8. Originally Posted by quzah
Don't start with code. Start with a description of the problem. Think about the description, and split the job into tasks. Split each job into smaller steps until you can't any further. Now think about how to make each step a line (or two, whatever) of code.

Let's say I needed to move everything out of my closets and onto the floor. Let me put that into steps:

1. Go to a closet.
2. Open it.
3. Take something out.
4. Throw it on the floor.
5. Do that until the closet is empty.
6. Do that for each closet.

There, see how easy that was?
Code:
```for each closet
open closet
for each item in the closet
take item out of closet
throw item on floor```

Quzah.
that's great and really helps, thanks

9. Sponi ... Start with the most obvious question... What has to happen first?
You can frotz around with all kinds of wierd arsed arrays and strange mathematical juggling...
But... what good are they if you have no data to juggle...

Step 1... you need to get at least one character from the user...

Ok... now you have something to work on... how you gonna do that?

10. Originally Posted by CommonTater
Sponi ... Start with the most obvious question... What has to happen first?
You can frotz around with all kinds of wierd arsed arrays and strange mathematical juggling...
But... what good are they if you have no data to juggle...

Step 1... you need to get at least one character from the user...

Ok... now you have something to work on... how you gonna do that?
well i figure something like
char ch;

printf("enter digit");
scanf("%c",&ch)

11. Ok, so now you've got a character...

What you gonna do with it?

12. Originally Posted by CommonTater
Ok, so now you've got a character...

What you gonna do with it?
Determine what type of character it is?

repeat the function to produce more input?

13. Originally Posted by sponi
Determine what type of character it is?

repeat the function to produce more input?
See... now that wasn't so hard was it....

Tap up some code, post it here and we'll see what we can do.

14. Originally Posted by CommonTater
See... now that wasn't so hard was it....

Tap up some code, post it here and we'll see what we can do.
Well there's the problem. I don't know how to determine what kind of character it is

Say i use variable char ch;

Do I just use some parameters to determine if it's a letter? ie if(ch>a && ch<z) ?

Also to count the amount of characters inputted of each type?

15. Originally Posted by sponi
Well there's the problem. I don't know how to determine what kind of character it is

Say i use variable char ch;

Do I just use some parameters to determine if it's a letter? ie if(ch>a && ch<z) ?
Now look at the 4th / 5th replies.