在字符串中找出连续最长数字串【经典】
2016-11-28 22:54
344 查看
在字符串中找出连续最长的数字串,并把这个串的长度返回,如果是个空串则返回“ ”;
例如sddsa12345asf123返回3;
注:我写的.cpp文件,已使用G++ [filename]编译运行。
gcc原则上也是可以编译c++文件,但实际操作中并没有自动链接c++的库,即使.cpp文件不包含任何C++头文件或特有语句,编译输出时还是会报错,需编译命令加-lstdc++;
例如sddsa12345asf123返回3;
#include <stdio.h> #include <malloc.h> #include <string.h> unsigned int CountStr(char **outputStr, char *inputStr) { if(NULL == inputStr || '\0' == inputStr) { *outputStr = ""; return 0; } int inod = 0; int maxLenth = 0; int tmpLenth = 0; for(int i = 0; inputStr[i] != '\0'; i++) { if(inputStr[i] >= '0' && inputStr[i] <= '9') { tmpLenth++; if(tmpLenth >= maxLenth) { inod = i; maxLenth = tmpLenth; } } else { tmpLenth = 0; } } if(maxLenth <= 0) { *outputStr = ""; return 0; } *outputStr = (char*)malloc(sizeof(char)*(maxLenth + 1)); strncpy(*outputStr, inputStr + inod -maxLenth + 1, maxLenth); (*outputStr)[maxLenth] = '\0'; return maxLenth; } int main() { char b[100], c[100]; int num; printf("请输入一个字符串\n"); scanf("%s", b); char *a = c; num = CountStr(&a, b); printf("数字串的最大长度为%d",num); return 0; }
注:我写的.cpp文件,已使用G++ [filename]编译运行。
gcc原则上也是可以编译c++文件,但实际操作中并没有自动链接c++的库,即使.cpp文件不包含任何C++头文件或特有语句,编译输出时还是会报错,需编译命令加-lstdc++;
相关文章推荐
- 华为OJ平台——在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 找数字串:找出字符串中连续最长的数字串
- 【校招笔试面试之字符串处理】在字符串中找出连续最长的数字串。
- 第25题: 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr),在字符串中找出连续最长的数字串,并把这个串的长度返回
- 在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 华为2018秋招编程题1:在字符串中找出连续最长的数字串
- 写正确函数需要注意的地方:在字符串中找出连续最长的数字串,并把这个串的长度返回
- 字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 25. 在字符串中找出连续最长的数字串
- 找出字符串中找出连续最长的数字串代码实现及思路
- 字符串操作系列_1在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字字符串的实例代码
- 字符串中找出连续最长的数字字符串的实例代码
- 华为OJ 在字符串中找出连续最长的数字串