int Get_str_Num_Count(const char* str)
{
int count = 0;
while (*str != '\0')
{
if (isdigit(*str) && !isdigit(*(str + 1)))//前一项是数字,其后一项不是数字
{
count++;
}
str++;
}
return count;
}
int* Get_str_Num(const char* str)
{
bool tag = false;
int n = Get_str_Num_Count(str);
int* arr = (int*)malloc(n * sizeof(int));
int sum = 0;
int k = 0;
while (*str != '\0')
{
if (isdigit(*str))
{
tag = true;
sum = sum * 10 + (*str - '0');
}
else
{
if (tag)
{
arr[k] = sum;
k++;
sum = 0;
tag = false;
}
}
str++;
}
if (tag)
{
arr[k] = sum;
}
return arr;
}
int main()
{
const char* str = "000A123x456 17960? 302ta0b5876";
int* p = Get_str_Num(str);
int len = Get_str_Num_Count(str);
for (int i = 0; i < len; i++)
{
printf("%d\n", p[i]);
}
return 0;
}
将一个字符串里面的连续数字作为一个整数,依次存放一数组中
最新推荐文章于 2023-05-17 12:14:34 发布