/*查找字符串中的数字串问题 输入一个字符串,查找它的第一个数字串,并返回其个数 */ #include <stdio.h> char firstnum(char *input,char *output) { char *in=input,*out=output,*temp; int count=0,i; while(*in !='\0') { if(*in>='0'&&*in<='9') { count=0; for(temp=in;*in>='0'&&*in<='9';in++) { count++; } if((*in>='a' && *in<='z')|| *in>='A' && *in<='Z') { break; } } in++; } for(i=0;i<count;i++) { *out++=*temp++; } *out='\0'; return count; } int main(void) { char input[] = "7892356asdfghjk123456789"; char output[1000] = {0}; int count = firstnum(input,output); printf("the str %s\n",output); printf("the count is %d \n",count); return 0; }
/work/ctest/interview$ ./1
the str 7892356
the count is 7