Like Tree1Likes

Count number of letters...

This is a discussion on Count number of letters... within the C Programming forums, part of the General Programming Boards category; Originally Posted by sponi Well there's the problem. I don't know how to determine what kind of character it is ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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) ?

    Also to count the amount of characters inputted of each type?
    That's one way to do it...

    You will need to do what real programmers do and look in your C Library Documentation for your compiler .... look for functions that help you classify characters as text, as digits, as whitespace etc... isdigit() is one.... what others can you find?
    Last edited by CommonTater; 09-12-2011 at 10:33 PM.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Now look at the 4th / 5th replies.
    Shhhhh... don't give away our secrets...
    Last edited by CommonTater; 09-12-2011 at 10:35 PM.

  3. #18
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Okay well, Im going to sleep soon, i'll try writing the program again and share my results in this thread tomorrow

    thanks everyone

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sponi View Post
    Okay well, Im going to sleep soon, i'll try writing the program again and share my results in this thread tomorrow

    thanks everyone
    Sounds good... At least you got past the roadblock.

    Remember, the first step in writing any program is to analyse the task.
    Then you plan what you're going to write (where you are now)
    Then you write the code
    Finally you test it...

  5. #20
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Hey guys, I finished the program i was stumped with. After rereading and thinking more about it, it became abundantly clear and following your useful advice CommomTater.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     char ch,a,b,c;
    
    main()
    {
        do{
        printf("\nEnter charater: ");
        ch=getche();
        switch(ch){
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            a++;
            break;
            case ';':
            case ':':
            case '.':
            case '>':
            case ',':
            case '<':
            case '/':
            case '?':
            case '"':
            b++;
            break;
            default :
            c++;
            }
        } while (ch!='\r');
    
        printf("There are %d numbers, %d punctuation, %d letters.",a,b,c);
    }
    Just wanted to say thanks, and I know this isn't the optimal way to do it but the exercise was supposed to use switch.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since we are implementing sub-optimal switch variations, here's mine:
    Code:
    switch( isdigit( c ) )
    {
        case 0: case EOF: break;
        default: a++; break;
    }
    switch( ispunct( c ) )
    {
        case 0: case EOF: break;
        default: b++; break;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    So how exactly does that work? what is EOF? I've only gotten through the first 3 sections of Teach Yourself C by Herbert Schildt

  8. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by sponi View Post
    So how exactly does that work? what is EOF? I've only gotten through the first 3 sections of Teach Yourself C by Herbert Schildt
    Well, there is your problem, right there.

    EOF is a special char reserved for specifying the end of an input stream, it stands for End of File. Quzah was merely pointing out there are standard library functions for doing what you hard coded; the ispunc(), isdigit(),isalpha()...functions that are available in the cctype library.
    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.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    EOF is a special char reserved for specifying the end of an input stream, it stands for End of File.
    Close but yet so far. It's not a char, it's a negative integer value that is specifically designed to not be represented as a char. That's why all of the functions that check individual characters take and return integers.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    Close but yet so far. It's not a char, it's a negative integer value that is specifically designed to not be represented as a char. That's why all of the functions that check individual characters take and return integers.
    Quzah.
    Haha...yes I did not mean char as in type, I should have just said value. Thank you for specifying.
    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.

Page 2 of 2 FirstFirst 12
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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21