华为oj:在字符串中找出连续最长的数字串
2015-08-04 16:29
162 查看
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "oj.h"
using namespace std;
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
char *pStart = intputstr;
int count = 0;
int maxlen = 0;
char *pTmp = NULL;
while(*pStart != '\0')
{
if (*pStart>='0' && *pStart<='9')
{
count++;
}
else
{
if (maxlen<=count)
{
maxlen = count;
pTmp = pStart - count;
}
}
pStart++;
}
if (maxlen==0)
{
const char a=' ';
return a;
}
else
{
char *pRes = (char*)malloc((maxlen+1)*sizeof(char));
strncpy(pRes, pTmp, maxlen);
pRes[maxlen] = '\0';
printf("%s", pRes);
free(pRes);
return maxlen;
}
return 0;
}
相关文章推荐
- Java下的Session监控:HttpSessionListener HttpSessionAttributeListener || HttpSessionBindingListener
- 【.Net码农】Server.MapPath()
- spring中的小知识点
- oracle之sqlplus讲解
- (自己的理解)面向对象和面向过程的区别
- 10+ Best Websites for Java Developers
- S3C2440 U盘,以及自动挂载顺序(inittab,fstab,mdev,hotplug)
- php性能优化
- android实现界面左右滑动(GridView动态设置item,支持每个item按某个属性排序来显示在不同的界面)
- 安卓实习第十五天
- Could not publish to the server.java.lang.NullPointerException
- C语言:两个顺序线性结构(数组)的并集
- linux文件系统如何进行文件存取
- system verilog中的类型转换(type casting)、位宽转换(size casting)和符号转换(sign casting)
- 第九章 执行上下文、执行上下文堆栈、作用域、闭包
- (leetcode) Reverse Bits
- C++ BigInteger模板
- 连接ORACLE客户端工具navicat111.12 for oracle
- Python3.4找不到urllib2
- 实现缓存功能