#include <stdlib.h>
#include "oj.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
char** pOutputstr;
char* intputstr = "abcd12345ed125ss123058789";
char* p = "abcd12345ed125ss123058789";
int max;
//通过pOutputstr改变P的指向
pOutputstr = &p;
max = Continumax(pOutputstr, intputstr);
printf("max = %d\n",max);
puts(p);
return 0;
}
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
int i,max,flag,startNum,count;
char * pMax;
char *temp;
int len = strlen(intputstr);
pMax = (char *)malloc(len+1);
i = 0;
max = 0;
while(intputstr[i]!='\0')
{
startNum = i;//记录开始有数字的位置
count = 0;
while(intputstr[i]>='0' && intputstr[i]<='9')
{
count++;
i++;
}
if((count != 0) && (max<=count))
{
max = count;
flag = startNum;
strncpy(pMax,intputstr+flag,max);
pMax[max] = '\0';
}
i++;
}
if(max == 0)
{
//如果连续最长的数字串的长度为0,应该返回空字符串
temp = (char *)malloc(10);
temp[0] = '\0';
*pOutputstr = temp;
}else
*pOutputstr = pMax;
return max;
}
#include "oj.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
char** pOutputstr;
char* intputstr = "abcd12345ed125ss123058789";
char* p = "abcd12345ed125ss123058789";
int max;
//通过pOutputstr改变P的指向
pOutputstr = &p;
max = Continumax(pOutputstr, intputstr);
printf("max = %d\n",max);
puts(p);
return 0;
}
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
int i,max,flag,startNum,count;
char * pMax;
char *temp;
int len = strlen(intputstr);
pMax = (char *)malloc(len+1);
i = 0;
max = 0;
while(intputstr[i]!='\0')
{
startNum = i;//记录开始有数字的位置
count = 0;
while(intputstr[i]>='0' && intputstr[i]<='9')
{
count++;
i++;
}
if((count != 0) && (max<=count))
{
max = count;
flag = startNum;
strncpy(pMax,intputstr+flag,max);
pMax[max] = '\0';
}
i++;
}
if(max == 0)
{
//如果连续最长的数字串的长度为0,应该返回空字符串
temp = (char *)malloc(10);
temp[0] = '\0';
*pOutputstr = temp;
}else
*pOutputstr = pMax;
return max;
}