C中字符串数组转换成整数数组

    xiaoxiao2023-10-09  143

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define N 9 long ctod( char *s ) { long d=0; while(*s) if(isdigit( *s)) { d=d*10+*s-'0'; s++; } return d; } long fun( char *a, char *b ) { return ctod(a)+ctod(b); } void main() { char s1[N],s2[N]; do { printf("Input string s1 : "); gets(s1); } while( strlen(s1)>N ); do { printf("Input string s2 : "); gets(s2); } while( strlen(s2)>N ); printf("The result is: %ld\n", fun(s1,s2) ); system("pause"); }

    此例是将两个字符串数字,转成整数数字,然后再将两个整数做相加的操作。

    例如字符串1234,和1111,转成整数1234和1111,然后相加=2345

    最新回复(0)