编程要求:实现查找字符串中最长数字子串的长度,并将其输出(可以改进:用while实现运行效率高)
#include<stdio.h>
#define MAX_SIZE 1024
int main()
{
void search_numstr();
char str1[MAX_SIZE]={0};
printf(“请输入字符串”);
gets(str1);
search_numstr(str1);
}
void search_numstr(char* str)
{
int i;
int j;
int x;
int y;
int count=0,len=0;
char str2[MAX_SIZE]={0};
for(i=0;(str+i) != ‘\0’;i++)
{
for(j=0;(str+i+j)>=‘0’&&*(str+i+j)<=‘9’;j++)
{
count++;//当前数字子串的长度
}
if(len<count)//如果找到了更长的数字子串
{
len =count;
for(x=0;x<len;x++)//每次都进行覆盖,留下最后最长的数字子串
{
*(str2+x) = *(str+x+i);
}
*(str2+len) = '\0';
}
count = 0;
}
printf("%d\n",len);
for(x=0;x<len;x++)
{
printf("%c",*(str2+x));
}
printf("\n");
}