c语言编程3
来源:百度知道 编辑:UC知道 时间:2024/07/08 14:20:59
编写一个程序实现如下功能:输入一个字符串,内有数字和非数字字符,如:a123x456 17960?302tab5876 将其中连续的数字作为一个整数,依次存放到一数组a中。例如123放在a[0]中,456放在a[1]中……统计共有多少个整数,并输出这些数(程序以文件名sy7_9.c存盘)。
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define LEN 100
void main()
{
char str[LEN], *p;
int i, cnt, num[LEN/2+1];
cnt = 0;
printf("请输入一个字串:");
gets(str);
p = str;
while (*p != '\0')
{
if (*p >= '0' && *p <= '9')
{
num[cnt++] = atoi(p);
/* 跳过已解释的数字字符 */
while (isdigit(*p)) ++p;
}
else
++p;
}
printf("字数个数:%d\n", cnt);
for (i = 0; i < cnt - 1; ++i)
printf("%d ", num[i]);
printf("%d\n", num[i]);
system("PAUSE");
}