首页 > 试题广场 >

求出一字符串中最长的数字串。比如as123cdf1234f,

[问答题]
求出一字符串中最长的数字串。比如as123cdf1234f,返回1234,char* FindNumber(char * out,char *in)。
#include 
#include 
#include 
using namespace std;

char* FindNumber(char * out, char *in) {
 if (!out || !in)
  return NULL;

 char *tmp = (char*)malloc(strlen(in) + 1);
 int n=0;
 strcpy(out, "");
 for (int i = 0; in[i] != '\0'; ++i) {
  if ((in[i] > '9' || in[i] < '0')&&n!=0) {
   tmp[n] = '\0';
   n = 0;
   if (strlen(out) < strlen(tmp))
    strcpy(out, tmp);
  }
  else {
   tmp[n++] = in[i];
  }
 }
 tmp[n] = '\0';
 if (strlen(out) < strlen(tmp))
  strcpy(out, tmp);
 free(tmp);
 return out;
}

int main() {
 char out[50];
 printf(FindNumber(out, "124567812151as123cdf1234f1233456"));
 return 0;
}
发表于 2017-08-08 20:46:15 回复(4)
char* FindNumber(char * out, char *in) {
    int i = 0;
    int max = 0;
    while (in[i] != '\0') {
        for (; in[i] != '\0' && in[i] < '0' || in[i] > '9'; ++i);
        int j = i;
        for (; in[i] != '\0' && in[j] >= '0' && in[j] <= '9'; ++j);
        int len = j - i;
        if (len > max) {
            delete []out;
            out = new char[len + 1];
            for (int n = 0; n < len; ++n) {
                out[n] = in[n + i];    
            }
            out[len] = '\0';
        }
        i = j;
        if (in[i] == '\0')
            break;
    }
    return out;
}
发表于 2017-09-17 21:36:23 回复(0)
char* FindNumber(char* out, char* in)
{
    int max = 0;
    char *begin = in, *end = NULL, *pos = NULL;
    while (*begin != '\0') {
        end = begin;
        while (*end != '\0' && isdigit(*end))
            ++end;
        if (end - begin > max) {
            max = end - begin;
            pos = begin;
        }
        begin = end > begin ? end : begin + 1;
    }
    if (max > 0) {
        memcpy(out, pos, max);
        return out;
    } else
        return NULL;
}


发表于 2016-09-05 21:46:29 回复(0)
/*
题目描述:
求出一字符串中最长的数字串。比如as123cdf1234f,返回1234,char* FindNumber(char * out,char *in)。
*/

#include <iostream>
#include <cstdlib>

using namespace std;

/*判断数字*/
bool isNum(char c)
{
	if (c >= '0' && c <= '9')
		return true;
	return false;
}

char* findNumber(char * out, char *in)
{
	if (!in || !out)
		return NULL;

	int maxLen = 0,len = 0;
	char tmp[100];

	for (char *p = in; *p != '\0'; ++p)
	{
		if (!isNum(*p))
		{
			if (len > maxLen)
			{
				maxLen = len;
				tmp[len] = '\0';
				strcpy(out, tmp);
			}//if
			tmp[0] = '\0';
			len = 0;
		}//if
		else{
			tmp[len++] = *p;
		}//else
	}//for

	/*判断最后一个数字串是否最长*/
	if (len > maxLen)
	{
		maxLen = len;
		tmp[len] = '\0';
		strcpy(out, tmp);
	}//if
	return out;
}

int main()
{
	char *in = new char[100];
	char *out = new char[100];

	cin >> in;
	out = findNumber(out, in);
	cout << out << endl;

	findNumber(out, in);
	cout << out << endl;
	delete in;
	delete out;
	system("pause");
	return 0;
}
发表于 2016-03-02 15:28:50 回复(1)
char* FindNumber(char * out,char *in)
{
	if(!in || !out)
		return NULL;

	char* p = in;
	char* address = out;
	string output,str_temp;
	unsigned int outLen = 0,len_temp = 0;

	while(*p != '\0')
	{
		if(*p >='0' && *p <='9')
			str_temp += *p;
		else{
			len_temp = str_temp.length();
			if(len_temp > outLen)
			{
				output = str_temp;
				outLen = len_temp;
			}
			str_temp.clear();
		}
		++ p;
	}
	len_temp = str_temp.length();
	if(outLen < len_temp)
	{
		output = str_temp;
		outLen = len_temp;
		str_temp.clear();
	}
	for(unsigned int i=0;i<outLen;i++)
		*out++ = output[i];
	*out = '\0';

	return address;
}

发表于 2015-10-13 14:10:04 回复(0)
char*FindNumber(char*in)
{
    vector<char>temp; vector<vector<char>>res;
       intk=0;
      for(char*p=out;*p!='\0';p++)
          if(*p<='9'&& *p>='0')
               temp[k++]=*p;
          elseif(temp.size()>0)
          {
                res.push_back(temp);
                k=0;
          }
        intcount=0;
        intindex=0;
        for(inti=0;i<res.size();i++) 
        {
             count=res[i].size();
             index=i;
             for(intj=i+1;j<res.size();j++) 
             {
                  if(count<res[j].size())
                  {
                       count=res[j].size();
                       index=j;
                  }
             }
        } 
        char*out=res[index];
        returnout; 
 }

发表于 2015-10-04 16:26:56 回复(0)
汀头像
#include<sdio.h>
#include<string.h>
char *FindNuber(char* out,char*in)
{
char *max=NULL,*p=NULL,*q=NULL;
int i=0,j=0;
p=in;
while(*p){
p++;
if((*p)>=0&&(*p)<=9)
{
q=p;
while((*p)>=0&&(*p)<=9)
{
i++;
p++;
}
}
if(i>j){
strncpy(max,q,i);
j=i;
i=0;
}
}
out=max;
return out;
}
编辑于 2015-05-02 15:23:17 回复(0)
/**
     * @author revoid
     */
    private static void method_2015_4_29_18_26() {

        String str = new Scanner(System.in).nextLine();
        
        char []ctrs = str.toCharArray();
        
        int maxLen = 0;
        
        String maxStr = null;
        
        for(int i = 0; i < ctrs.length; i++)
        {
            int pos = 0;
            int count = 0;
            
            if(ctrs[i] >= '1' && ctrs[i] <= '9')
            {
                pos = i;
            }
            
            int endPos = 0;
            
            for(int j = pos; j < ctrs.length; j++)
            {
                if(ctrs[j] >= '1' && ctrs[j] <= '9')
                {
                    count++;
                }
                else
                {
                    endPos = j;
                    break;
                }
            }
            
            if(count > maxLen)
            {
                maxLen = count;
                
                maxStr = str.substring(endPos - count, endPos);
            }
        }
        
        System.out.println(maxStr + ":" + maxLen);
    }

发表于 2015-04-29 18:14:51 回复(0)
如此简单
发表于 2015-04-19 20:26:52 回复(0)
char* FindNumber2(char * out, char * in)
{
        char cTemp = '\0';
        int iStart1 = -1;
        int iEnd1 = -1;
        int iStart2 = -1;
        int iEnd2 = -1;
        char * in_temp = in;
        int iIndex = 0;
        while ((cTemp = *in_temp++) != '\0')
        {
                printf("cTemp = %c", cTemp);
                if ((cTemp - 48) <= 9 && (cTemp - 48) >= 0)
                {
                        if ((iStart2 == -1) && (iEnd2 == -1))
                        {
                                iStart2 = iIndex;
                                iEnd2 = iIndex;
                        }
                        else
                        {
                                iEnd2 = iIndex;
                        }
                }
                else
                {
                        iStart1 = (iStart1 > iStart2) ? iStart1 : iStart2;
                        iEnd1 = (iEnd1 > iEnd2) ? iEnd1 : iEnd2;
                        iStart2 = iEnd2 = -1;
                }


                iIndex++;
        }

        iStart1 = (iStart1 > iStart2) ? iStart1 : iStart2;
        iEnd1 = (iEnd1 > iEnd2) ? iEnd1 : iEnd2;

        printf("iStart1 = %d; iEnd1 = %d\n", iStart1, iEnd1);
        if(iStart1 >-1)
        {
        strncpy(out, in + iStart1, iEnd1 - iStart1+1);
        printf("out = %s \n", out);
        }
        return out;
}
我怎么写了那么长, 有更好的吗?
发表于 2014-12-11 17:17:17 回复(0)
234234
发表于 2014-11-28 13:12:51 回复(0)
easy
发表于 2014-11-09 18:15:26 回复(0)