Minor problem, hope someone can help

This is a discussion on Minor problem, hope someone can help within the C Programming forums, part of the General Programming Boards category; Im new to C, and have been teaching myself for a few days now. I'm trying to write some code ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    15

    Smile Minor problem [Resolved]

    Im new to C, and have been teaching myself for a few days now. I'm trying to write some code which will reverse the digits of a number provided as an argument when the code is run from the command line. Here's what I've come up with:

    Code:
    /*
     *  Test.c
     *  lab2
     *
     *  Created by ** on 14/01/09.
     *  Copyright 2009 UWA. All rights reserved.
     *
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int reverse(char s[])
    {
    	int length = strlen(s);
    	int y = atoi(s);
    	for (int r = 0; r < length; r++)
    	{
    		s[r] = (y % 10);
    		y = (y / 10);
    	}
    	return atoi(s);
    }
    
    int main(int argc, char *argv[])
    {
    	int y = (reverse(argv[1]));
    	printf("%d\n", y);
    	return 0;
    }
    I have tested it as much as I can and from what I can see, the string s[r] is being filled correctly with the input string numbers, but in reverse, though for some reason when i return the integer equivalent of this string using atoi, I always get 0 as the response.
    Help would be much appreciated. Thankyou.
    Last edited by h4rrison.james; 01-14-2009 at 08:22 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The odds that a digit from 0 to 9 is a valid character is slim, if not none. You need to fill s with the characters '0' through '9' not the numbers 0 through 9. (Note that C does guarantee that the digits are in order in your character set, so '0'+0 is '0', '0'+1 is '1', etc.)

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    15
    Of course.

    Thanks for your help.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by h4rrison.james View Post
    Of course.

    Thanks for your help.
    The other option, of course, is to not modify the s[] input (which is nice to the caller, as the caller may need to make a copy, which is extra work), and just calculate (using multiplication by 10 and addition) to produce the integer - and it's probably no slower than using atoi(). It would then also work to do something like this:
    Code:
    x = reverse("12345");
    which won't work on many modern OS's where a string literal (in this case "12345") is not writable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hi im new..hope u can help me..
    By dude123 in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 06:33 AM
  2. Learned optimism
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-25-2003, 03:06 PM
  3. Bob Hope, dead at 100 of being really old
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-28-2003, 08:04 AM
  4. last part of program i hope to validate
    By sturm100 in forum C Programming
    Replies: 8
    Last Post: 07-10-2002, 06:51 AM
  5. a math problem . . . i hope atleast
    By blight2c in forum C++ Programming
    Replies: 7
    Last Post: 05-26-2002, 02:23 PM

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