c-moodle/homework/11977.c

39 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <string.h>
int is[100005]; // 存放整数的数组
char s[100005]; // 输入的字符串
// 找出字符串中的整数并存入数组a中
int findint(char* s, int* a) {
int k = 0; // 记录数字的个数
int ls[5555] = {0}; // 存放每个数字的每一位
for (int i = 0; i < strlen(s); i++) {
int j = 0;
while (s[i + j] >= '0' && s[i + j] <= '9') { // 找到一个数字的每一位
ls[j] = s[i + j] - '0';
j++;
}
if (!j) // 如果没有找到数字,继续循环
continue;
i += j - 1; // 跳过已经找到的数字
int num = 0;
for (int l = 0; l < j; l++) {
num = num * 10 + ls[l]; // 将每一位数字转换成整数
}
a[k++] = num; // 将整数存入数组并将数组下标加1
}
return k; // 返回数字个数
}
int main() {
gets(s); // 输入字符串
int k = findint(s, is); // 调用函数找出整数
for (int i = k - 1; i >= 0; i--) { // 倒序输出整数
printf("%d", is[i]);
if (i != 0) // 如果不是最后一个数字,加上逗号
printf(",");
}
}