Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1411711
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-20 10:35:25

点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <malloc.h>
  6. #include "oj.h"


  7. /* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
  8. 函数原型:
  9.    unsigned int Continumax(char** pOutputstr, char* intputstr)
  10. 输入参数:
  11.    char* intputstr 输入字符串
  12. 输出参数:
  13.    char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
  14.    pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放

  15. 返回值:
  16.   连续最长的数字串的长度

  17.  */
  18. unsigned int Continumax(char** pOutputstr, char* intputstr)
  19. {
  20.     char* pOut = NULL;
  21.     char* pIn = intputstr;
  22.     if (NULL == intputstr)
  23.     {
  24.         pOut = (char*)malloc(sizeof(char));
  25.         *pOut='\0';
  26.         return 0;
  27.     }

  28.     int i,tempIndex,len,maxNum=0; //tempIndex为下标的副本,len为输入字符串的长度,maxNum为长度的最大值
  29.     for (len=0;intputstr[len]!=0;len++);//求输入字符串长度

  30.     *pOutputstr = (char*)malloc(len+1);
  31.     pOut = * pOutputstr;

  32.     for (i=0;i<len;i++)
  33.     {
  34.         if (isdigit(intputstr[i]))
  35.         {
  36.             tempIndex=i;
  37.             
  38.             for (i++;i<len&&isdigit(intputstr[i]);i++);
  39.             if (maxNum<i-tempIndex+1)
  40.             {
  41.                 maxNum=i-tempIndex;
  42.                 memcpy(pOut,intputstr+tempIndex,maxNum);                
  43.                 pOut[maxNum]='\0';
  44.             }
  45.         }
  46.     }
  47.     if (maxNum==0)
  48.     {
  49.         pOut[maxNum]='\0';
  50.     }
  51.     return maxNum;
  52. }

阅读(1055) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~