c语言程序功能:输入一个字符串,编程实现将其中字母字符放在前面,数字字符放在后面生成一个新串,并输出。 运行示例: 输入:ab23ad34fhtt7 输出:abadfhtt23347
时间: 2024-02-13 10:07:51 浏览: 33
好的,你可以参考以下 C 语言代码实现:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char s[100], letters[100], digits[100], new_str[100];
int i, j = 0, k = 0;
printf("请输入一个字符串:");
scanf("%s", s);
for (i = 0; i < strlen(s); i++) {
if (isalpha(s[i])) {
letters[j++] = s[i];
} else if (isdigit(s[i])) {
digits[k++] = s[i];
}
}
strcpy(new_str, letters);
strcat(new_str, digits);
printf("新字符串为:%s\n", new_str);
return 0;
}
```
代码中,我们先定义了三个字符数组 `s`、`letters` 和 `digits`,分别用于存储输入的字符串和字母、数字字符。然后遍历输入的字符串 `s` 中的每个字符,如果是字母字符,则放入 `letters` 中,如果是数字字符,则放入 `digits` 中。最后将 `letters` 和 `digits` 拼接在一起,得到新的字符串并输出。
输入示例:`ab23ad34fhtt7`,输出示例:`abadfhtt23347`。
希望能够帮到你。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)