+ Reply to Thread
Results 1 to 4 of 4

Thread: Can anyone solve this question in C programming Language?

  1. #1
    Stella Richards is offline x10Hosting Member Stella Richards is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    1

    Can anyone solve this question in C programming Language?

    Hello,


    Write code to solve the following problem. Define a function that takes a String as a parameter and returns an integer value. The function need to loop through each character in the input string to find a value of “x”. If a value of “x” is found in the string then return the position within the string where “x” is found else return -1.

  2. #2
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: Can anyone solve this question in C programming Language?

    you might want to look up the atoi function (ascii to integer) and you might also want to do your homework yourself ;)

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Can anyone solve this question in C programming Language?

    It sounds like more like a stristr (from php, I don't remember the C equivalent if there is one).

    If I understand correctly, this function finds the letter "x" in a string and returns it's position. I'll try to get you started, I think you'll understand this better if you do most of it yourself

    Code:
    // Pseudo-code (will not compile in any language :P)
    function findX(String)
        for i = 0 to length(String)
            if ... // figure out how to find what letter is at position "i" (your loop counter)
                return i
            else
                return -1
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Can anyone solve this question in C programming Language?

    Quote Originally Posted by garrettroyce View Post
    It sounds like more like a stristr (from php, I don't remember the C equivalent if there is one).
    strchr(). There's also strstr() and strcasestr() in the std C library. Many of those basic PHP funcs are just wrappers for the C funcs.

    @Stella Richards: normally in a case like this, I'd just link to "How to ask questions the smart way", but this is important enough to quote here:
    Don't post homework questions

    Hackers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.

    If you suspect you have been passed a homework question, but can't solve it anyway, try asking in a user group forum or (as a last resort) in a “user” list/forum of a project. While the hackers will spot it, some of the advanced users may at least give you a hint.
    You'd do well to read the rest of the document.

    The functions garretroyce and I mentioned would be useful as comparison to check that your function works. You should also check if your language of choice supports foreach loops. Using foreach, you might be able to write a more general function that doesn't assume integer indices.

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2009, 07:23 AM
  2. Choice of Programming Language
    By shanrei in forum Computers & Technology
    Replies: 10
    Last Post: 12-02-2008, 10:27 PM
  3. Your Favorite Programming Language
    By sumitmehta in forum Off Topic
    Replies: 14
    Last Post: 03-08-2008, 11:26 PM
  4. Replies: 18
    Last Post: 02-01-2008, 12:24 PM
  5. Common Programming Terms --- Part I
    By Tariqul Islam in forum Tutorials
    Replies: 2
    Last Post: 01-19-2008, 12:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers