統計一個輸入字符串中最長的連續數字的個數,并提取此最長數字串復制至另一數組。
例如輸入字符串"a123b1234567c9d",則函數應該返回7,并復制"1234567"到另一個數組中去。實現如下:
#include <stdio.h>
#include <string.h>
unsigned int ContinuousMax(const char *szStr, char *szContinuousStr)
{
//
const char *ptr = szStr;
const char *Temp = NULL;
const char *Final = NULL;
unsigned int Count = 0, Max = 0;
while(*ptr != '/0')
{
//如果是數字
if(*ptr >= 0x30 && *ptr <= 0x39)
{
for(Temp = ptr; *ptr >= 0x30 && *ptr <= 0x39; ptr++)
{
Count++;
}
}
else
{
ptr++;
}
if(Count > Max)
{
Max = Count;
Count = 0;
Final = Temp;
}
}
strncpy(szContinuousStr, Final, Max);
*(szContinuousStr + Max)= '/0';
return Max;
}
本文介绍了一个C语言函数,该函数能够找出输入字符串中最长的连续数字串,并将其复制到新的数组中。通过遍历字符并检查ASCII值来判断是否为数字。
2564

被折叠的 条评论
为什么被折叠?



