1. 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
#include
#include
int continumax(char *outputstr, char *inputstr)
{
char *psrc;
char *dest;
int len, addr = 0, count = 1, tmp1, tmp2;
char start_flag = 0;
int max_len = 0;
int dest_addr = 0;
psrc = inputstr;
dest = outputstr;
len = strlen(psrc);
//tmp1 = *psrc
while(addr < len)
{
tmp1 = *(psrc+addr);
addr++;
tmp2 = *(psrc+addr);
if(tmp2 == tmp1 + 1)
{
start_flag = 1;
if(addr == len)
{
if(count > max_len)
{
max_len = count;
dest_addr = addr - max_len;
}
}
}
else
{
if(count > max_len)
{
max_len = count;
dest_addr = addr - max_len;
}
start_flag = 0;
count = 1;
}
if(start_flag)
count++;
}
strncpy(dest, psrc+dest_addr, max_len);
return max_len;
}
int main(int argc, char *argv[])
{
char outputstr[100];
if(argc == 2)
continumax(outputstr, argv[1]);
else
{
printf("args error\n");
return 1;
}
printf("%s\n", outputstr);
return 0;
}
阅读(1006) | 评论(0) | 转发(0) |