[Date Prev][Date Next][Date Index]
[intv] string to int
#include <stdio.h>
//Obj: Convert a String to Int
//Compile as: gcc -o test test.c
int strtoint(char *);
int main(void){
char *str = "-4321";
printf("String = %s, Converted to = %d \n", str, strtoint(str));
}
int strtoint(char *instr){
int j = 0;
int mul = 1;
int initValue = 0;
int retValue = 0;
if (instr[0] == '-'){
mul = -1;
initValue = 1;
}
for (j = initValue; j < strlen(instr); j++){
retValue *= 10;
retValue += instr[j] - '0';
}
return mul*retValue;
}
Comments and corrections are appreciated and can be sent to
papers@mia.ece.uic.edu.
Click here for ©opyright information.
|