Thread: Counting characters based on certain parameters

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    43

    Counting characters based on certain parameters

    Hello guys,

    I was wondering, how do you build a program that will assign certain values to certain characters and different values to others?

    For example, if I wanted to assign a value of 1 to vowels and 2 to consonants, how exactly would I go about doing this? And how would I make it so it ignores any other characters (!@#%#$^ etc.) ?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    You can implement 2 arrays, one for the 1-valued characters, and other for the 2-valued; then simply read char by char and check out on which array is; for the ignored characters, if they all are non vowels nor consonants you can use a simple ascii trick instead of search them on the arrays.

    Hope that helps

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Thank you for the response, Niara. It sheds a little light, but I am still very new to C programming unfortunately and so I am still a little lost.

    Is there a resource you can guide me to that will help me out on such matters? Also, the value will be based on user input; they will be able to decide how much a vowel/consonant is worth. And how do I use ASCII values in the search instead?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Ehh... Is the user going to be asked a question and then he, or she, is supposed to answer. Like
    You favourite superhero.
    (1) Kermit the frog
    (2) Winnie the Poo
    (3) Hulk
    Or... are you going to encode what they type in?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Basically the program operates like this:

    Enter value for Vowels: %d \n
    Enter value for Consonants: %d \n

    Enter string: %s \n

    Total for vowels: %d \n
    Total for consonants: %d \n
    ---------------------------
    It calculates a total for each vowel*inputvalue and likewise for consonants

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Write these functions:
    Code:
    int isVowel(char ch) {
        /* Returns 1 if ch is a vowel
         * otherwise returns 0 */
    }
    
    int isConsonant(char ch) {
        /* Returns 1 if ch is a consonant
         * otherwise returns 0 */
    }
    Then write your main program using those functions.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    I'd have thought it'd make more sense to just have 1 function

    Code:
    int getLetterValue(char ch)
    {
       return 1 if vowel
       return 2 if consonant
       return 0 otherwise
    }
    In answer to your original question OP, no, there isn't really a way you can do what you're asking. Probably the quickest (in terms of exec time) would be to have an array of 26 values, indexed by letter and containing the value. So it'd be

    Code:
    {1,2,2,2,1 ...)
    You'd have to handle accesses outside of the array (e.g. digits, special characters), and do a bit of calculation on the input ASCII value to get it into 0-25 range. Nothing very tricky, but not trivial. It may be better to just implement a large switch statement in getLetterValue. Don't forget to handle both upper and lower case, or convert to all one case first.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by smokeyangel View Post
    I'd have thought it'd make more sense to just have 1 function
    I concur. However, I was attempting to assess the OP's skill level. If they can't correctly create the isVowel function, then anything more is a pipe dream.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    you could simply create the TOUPPER macro that converts all the alphabets to upper case and then use the array that designates the case provided by @smokeyangel together with the ascii trick successfully check the number of consonants and vowels .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help counting characters
    By dom89 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 10:25 PM
  2. Counting Characters
    By jrdoran in forum C Programming
    Replies: 2
    Last Post: 11-28-2006, 08:06 PM
  3. Counting characters
    By jmajeremy in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 10:14 AM
  4. Counting characters
    By Calavera in forum C Programming
    Replies: 5
    Last Post: 10-01-2004, 10:15 AM
  5. Counting characters
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-29-2003, 12:54 PM