Thread: Count number of letters...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    10

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    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. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, I would start by taking a look at the following:
    All of which can be found by looking through the cctype header file
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by novacain View Post
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sponi View Post
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    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. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    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. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, so now you've got a character...

    What you gonna do with it?

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    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. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sponi View Post
    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. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    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. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by sponi View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-05-2010, 03:04 AM
  2. Count Number of Letters in a Word
    By quiksand in forum C Programming
    Replies: 10
    Last Post: 05-14-2010, 11:44 PM
  3. Count number of letters and numbers in a file
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 04-21-2008, 01:33 AM
  4. need to count individual letters in input
    By epox in forum C Programming
    Replies: 12
    Last Post: 05-22-2006, 06:32 AM
  5. Count the letters of a string.
    By blackjs in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 10:15 PM

Tags for this Thread