开发者

String parsing in C

how would you parse the string, 1234567 into ind开发者_高级运维ividual numbers?


char mystring[] = "1234567";

Each digit is going to be mystring[n] - '0'.


What Delan said. Also, it's probably bad practice for maintainability to use a ASCII dependent trickery. Try using this one from the standard library:

int atoi ( const char * str );

EDIT: Much better idea (the one above has been pointed out to me as being a slow way to do it) Put a function like this in:

int ASCIIdigitToInt(char c){
    return (int) c - '0';
}

and iterate this along your string.


Don't forget that a string in C is actually an array of type 'char'. You could walk through the array, and grab each individual character by array index and subtract from that character's ascii value the ascii value of '0' (which can be represented by '0').

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜