#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main(){
char s[255], *t = s, *start = s;
int find_digit = 0;
long num = 0;
printf("Please input a strings(include digits): "); /* 请输入 1 个含有数字的字符串 */
gets(s);
printf("\n");
while (*t != '\0'){
if (isdigit(*t)){
if (!find_digit) start = t;
find_digit = 1;
t++;}
else if (*t == ' ') strcpy(t, t + 1); /* 删除空格 */
else if (find_digit){
*t = '\0';
break; }
else t++;
}
if (!find_digit)
printf("NO any digit in the string!\n");
else{
printf("The digit form in the string is %s\n", start); /* 字符串里的数字部分 */
/* 字符串里的数字部分转换为真正的整数 */
while (*start != '\0'){
num *= 10;
num += (int)(*start - '0');
start++;
}
printf("The number from string is %ld\n", num); /* 输出真正的整数 */
}
printf("Press any key to continue...\n");
getch();
}
温馨提示:内容为网友见解,仅供参考