Thread: Strings question

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Strings question

    I am doing a program to count even ASCII characters in a string for instance, when the string is input "ABCDE", there is a recursive function within the program to display 2 where both B and D are even ASCII characters. Now my question is how and what function you use to examine the characters of a string one by one to see if its an even ASCII character?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The modulus (%) operator.

    Code:
    char cVal;
    ...
    if( !(cVal % 2) )
    {
        // It is an even character
    }
    else
    {
        // It is an odd character
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    what is the function cVal for? Sorry but the function should be input by string but can the function accept string?
    Last edited by kimimaro; 03-14-2005 at 12:18 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    cVal is not a function, its the character you are testing to see if it is even or odd.
    When no one helps you out. Call google();

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by kimimaro
    what is the function cVal for?
    cVal is not a function, it is a simple char data type and just used as an example of how to test whether the given character is odd or even. In your actual code you would be testing whatever character you were currently looking at in your "string".

    Quote Originally Posted by kimimaro
    Sorry but the function should be input by string but can the function accept string?
    This is the c-board and not the c++ board. By "string" I take it to mean you are dealing with a NULL terminated character array? Why don't you show us some of the code you are currently working on?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    I think what he is trying to do is, input A or C or any character for that matter, and find if the numeric value of A for example is even.
    When no one helps you out. Call google();

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    yes and the question that I'm dealing with is that it stated that I must use Recursive function and must accept string value. From my senese is that the input should measure the length of the string and then the recursive will stop after all the numbers of character of the string is finished. I dont know how to use the strlen function eventhough I've read it in the tuitorial and I dont know what function should I use to one-by-one operates the characters within the single string

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    That question is as below :

    Write a recursive function called CountEvenASCII that takes in a string parameter str and return the number of characters with even ASCII codes. For example CountEvenASCII("ABCDE") returns 2 (as B and D are of even ASCII codes).

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Well do you have anything done so far? Do you know how to find the numeric value of A or B or C. Do you know how to find if those values are even? Do the easy things first and then figure out how to make everything work.
    When no one helps you out. Call google();

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    I've done the recursive and the putchar function so far. To find even numbers, (str%2==0)
    But I am no good with strings what I want now is how to get each alphabet in the string to check if its even

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The exact same way. Do you know how to move through an array? If so, then do so, and compare that character to your even/odd test.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 06:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 09:11 PM