用C语言写一个函数,该函数从主程序接受一个字符串,将字符串中的数字和其它字符分开,形成二个字符串输出。
时间: 2024-03-15 15:46:38 浏览: 19
好的,这是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void split_string(char *str, char *numbers, char *letters) {
int i, j, k;
j = k = 0;
for (i = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
numbers[j++] = str[i];
} else if (isalpha(str[i])) {
letters[k++] = str[i];
}
}
numbers[j] = '\0';
letters[k] = '\0';
}
int main() {
char str[100], numbers[100], letters[100];
printf("Enter a string: ");
scanf("%s", str);
split_string(str, numbers, letters);
printf("Numbers: %s\n", numbers);
printf("Letters: %s\n", letters);
return 0;
}
```
这个函数接受一个字符串 `str`,并将其中的数字存储在 `numbers` 字符串中,将其中的字母存储在 `letters` 字符串中。使用 `isdigit` 和 `isalpha` 函数判断字符是数字还是字母。注意,如果两个字符串的长度超过 100,可能会导致缓冲区溢出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)