going reverse in a for loop?
Posted
by sil3nt
on Stack Overflow
See other posts from Stack Overflow
or by sil3nt
Published on 2010-03-21T02:21:50Z
Indexed on
2010/03/21
2:31 UTC
Read the original article
Hit count: 408
Hello there, Basically i got this for loop and i want the number inputed (eg. 123) to be printed out in reverse, so "321". so far it works fine and prints out the correct order when the for loop is
for(i = 0; i<len ; i++)
but i get an error when i try to print it in reverse?. Whats going wrong?
#include <stdio.h>
#include <string.h>
void cnvrter(char *number);
int main(){
char number[80];
printf("enter a number ");
gets(number);
cnvrter(number);
return 0;
}
void cnvrter(char *number){
char tmp[80];
int i = 0,len = 0;
int cnvrtd_digit = 0;
len = strlen(number);
printf("\nsize of input %d\n",len);
for(i = len; i>len ; i--){
if ( ( number[i] >= '0' ) && ( number[i]<='9' ) ){
tmp[0] = number[i];
sscanf(tmp,"%d",&cnvrtd_digit);
}
printf("%d\n",cnvrtd_digit);
}
}
© Stack Overflow or respective owner