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:
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.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; }
Help would be much appreciated. Thankyou.