Hi could someone point me out what's wrong with this code ?
#include <stdio.h>
int convstrg(char* str) {
int output = 0;
char* p = str;
for (int i=0;str[i]!='\0';i++) {
char c = *p++;
if (c < '0' || c > '9')
continue;
output *= 10;
output += c - '0';
}
return output;
}
int main(){
char x[] = "1xx23";
printf("%d\n", convstrg(x));
return 0;
}
The code should return an integer when the output is string integer. But it seems i getting weird number such as 0.
this are few test case, some of it works some didn't
"123" -> 123
"23xyz" -> 23
"" -> 0
"abc" -> 0
"-1" -> -1
Thanks
EDIT
Ok now i sort out all the cases expect for negative string..