首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

在一个字符串中查寻最长的一连串数字

2012-10-10 
在一个字符串中查找最长的一连串数字这个问题想了很久 始终写不出来 自己写了一下 还是不行请教一下各位大

在一个字符串中查找最长的一连串数字
这个问题想了很久 始终写不出来 自己写了一下 还是不行 请教一下各位大牛 指点一下 我是新手 比较菜鸟
比如说:“djfkdjfkjg123dafddf123456”
查找出这个字符串中 最长的数字字符串是 6位 并输出123456 


[解决办法]
很早以前写过的一个小程序,随便简单改了一下来解决你的问题:

C/C++ code
//================================================================= #include <string.h>    #include <iostream.h> void my_strcat(char output[],char input[],int from,int to){    int i=from;    static count = 0;    while (i<=to)    {        output[count] = input[i];        input[i] = ' ';        i++;        count++;        }    output[count] = ' ';    count++;    }int sort_string(char input[],char output[]){    int beg,end,len,is_last_char,i,max,from,to;    beg=end=len=is_last_char=i=max=from=to = 0;    max = 0;        while (1)    {        if( input[i]>='1' && input[i]<='9')        {            if (is_last_char==1)            {                len++;            }            else            {                beg = i;            }            is_last_char = 1;        }        else        {            if (is_last_char==1)            {                end = i-1;                if (len>max)                {                    max = len;                    from = beg;                    to = end;                }                }            len = 0;            is_last_char = 0;        }        //--------------------------------        i++;        if (input[i]=='\0')        {            if (is_last_char==1)            {                if (i-beg>max)                {                    max = len;                    from = beg;                    to = i-1;                }            }            //把最长字符串拷到output中,把对于input中的部分替换了            my_strcat(output,input,from,to);            break;        }    }    if (max==0)    {        return 0;    }    //return sort_string( input, output);    }void main(){    char input[]="djfkdjfkjg123dafddf123456";    char output[]="djfkdjfkjg123dafddf123456";    memset(output,0,sizeof(output));    cout<< input <<endl;    cout<< "------------------------------------------------------" <<endl;        sort_string( input, output);            cout<< output <<endl;    }
[解决办法]
1 #include <stdio.h>

3 int main()
4 {
5 char a[100], *p;
6 int num, max;
7 while(p=a, *gets(p))
8 {
9 max = num = 0;
 10 while(*p) 
 11 {
 12 if(*p >= '0' && *p <= '9')
 13 {
 14 num = num*10 + *p - '0';
 15 }
 16 else
 17 {
 18 if(num > max) max = num;
 19 num = 0; 
 20 }
 21 p++;
 22 }
 23 if(num > max) max = num;
 24 printf("%d\n", max);
 25 }
 26 return 0;
 27 }

对于给定长度的字符串和小于int范围的数字,这程序应该不成问题吧。。。
[解决办法]
24 printf("%d\n", max);

有啊,你运行了???
asdfjie2354jie89
2354
dfjie2348afef3123iji
3123
1234jife898
1234

这就是我的结果,倒是没有计算长度
------解决方案--------------------


当字符串的长度超过十位的时候,你的就不行了吧?
这个是我写的,求批评!!!!!

C/C++ code
#include <stdlib.h>#include <string.h>#include <stdio.h>void the_longest_substring(char *s){    char *sub_string = (char*)malloc(strlen(s));    char *start = s;    char *end;    int len = 0;    while(*start!='\0')    {        while(*start!='\0' && (*start>'9' || *start<'0'))++start;        end = start;        while(*end!='\0' && (*end<='9' && *end>='0'))++end;        if(end-start>len)        {            int i;            for(i=0; i<end-start; ++i)            {                sub_string[i] = *(start+i);            }            len = end-start;        }        start = end;    }    int i;    for(i=0; i<len; ++i)    {        printf("%c", sub_string[i]);    }    free(sub_string);}void main(){    char *s = "djfkdjfkjg123dafddf123456";    the_longest_substring(s);    printf("\n");    return ;}
[解决办法]
你试试这个“12345678910”输入
探讨
1 #include <stdio.h>
2
3 int main()
4 {
5 char a[100], *p;
6 int num, max;
7 while(p=a, *gets(p))
8 {
9 max = num = 0;
10 while(*p)
11 {
12 if(*p >= '0' &amp;&amp; *p <……

[解决办法]
探讨

当字符串的长度超过十位的时候,你的就不行了吧?
这个是我写的,求批评!!!!!
C/C++ code

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void the_longest_substring(char *s)
{
char *sub_string = (char*)malloc(strlen(s)……

[解决办法]
你的代码中有两处出现“max=num;”在“max=num;”后面加上
int j=0;
for(j=0; j<num; ++j)
{
outputstr[j] = intputstr[i-num+j];
}

还有就是最后的两行输出语句你都没加%,我给你改了如下,替换一下就可以了。
printf("最长的个数为%d\n",c);
printf("该字符串为%s\n",outputstr);

[解决办法]
C/C++ code
void func(const char *str){    int len = 0;    int max_len = 0;    const char *max_pos = str;        for (;;)    {        if ('0' <= *str && *str <= '9')        {            ++len;        }        else        {            if (len > max_len)            {                max_len = len;                max_pos = str - len;            }            len = 0;            if (0 == *str)            {                break;            }        }        ++str;    }        printf("%s, %d\n", max_pos, max_len);}
[解决办法]
稍微修改一下
void func(const char *str)
{
int len = 0;
int max_len = 0;
const char *max_pos = str;

for (;;)
{
if ('0' <= *str && *str <= '9')
{
++len;
}
else
{
if (len > max_len)
{
max_len = len;
max_pos = str - len;
}
len = 0;
if (0 == *str)
{
break;
}
}
++str;
}

printf("%d\n", max_len);
while(*max_pos!='\0')
{
printf("%c", *max_pos);
++max_pos;
}
}

探讨
C/C++ code

void func(const char *str)
{
int len = 0;
int max_len = 0;
const char *max_pos = str;

for (;;)
{


if ('0' <= *str &amp;&amp; *str <= '9')
……


[解决办法]
刚才的那个有点问题,又改了一下,这下没问题了
C/C++ code
void func(const char *str){    int len = 0;    int max_len = 0;    const char *max_pos = str;        for (;;)    {        if ('0' <= *str && *str <= '9')        {            ++len;        }        else        {            if (len > max_len)            {                max_len = len;                max_pos = str - len;            }            len = 0;            if (0 == *str)            {                break;            }        }        ++str;    }        printf("%d\n", max_len);    while('0' <= *max_pos && *max_pos <= '9')    {        printf("%c", *max_pos);        ++max_pos;    }    }
[解决办法]
C/C++ code
#include<iostream>#include<string>using namespace std;int main(){    char str[100];    while(scanf("%s",str)==1){        int b=0,e=0,s=0;        int begin=0,end=-1,len=0;        for(int i=0;str[i];++i){            if(str[i]<='9' && str[i]>='0'){                s++;                e=i;            }            else{                 b=i+1;                 s=0;            }            if(s>len){                begin=b;                end=e;                len=s;            }        }        printf("len== %d \n",len);        for(int i=begin;i<=end;++i)            printf("%c",str[i]);        puts("");    }    return 0;} 

热点排行