编写程序实现,任意输入一个字符串s(长度<=80),将s中的所有数字字符移到所有非数字字符之后,并保持所有数字字符和非数字字符原有的先后顺序,并输出。例如形参s的值为abc12def3,则执行结果为abcdef123。
时间: 2024-05-08 16:16:16 浏览: 10
```python
def move_digits(s):
digits = []
non_digits = []
for char in s:
if char.isdigit():
digits.append(char)
else:
non_digits.append(char)
return ''.join(non_digits + digits)
```
测试:
```python
s = 'abc12def3'
print(move_digits(s)) # 输出:abcdef123
```
相关问题
输入任意字符组成的一个字符串(长度<=100),将其反序输出。
好的,下面是用 C++ 语言编写的代码:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str;
cout << "请输入一个字符串:";
getline(cin, str);
reverse(str.begin(), str.end());
cout << "反序输出的字符串为:" << str << endl;
return 0;
}
```
这段代码首先通过 `getline` 函数获取用户输入的字符串,并使用 `reverse` 函数将字符串翻转,最后输出翻转后的字符串。例如,输入字符串 "Hello World!",则输出 "!dlroW olleH"。
输入任意字符组成的一个字符串(长度<=100),将其反序输出。用C语言
好的,下面是用 C 语言编写的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[101];
printf("请输入一个字符串:");
scanf("%s", str);
int len = strlen(str);
printf("反序输出的字符串为:");
for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
这段代码首先通过 `scanf` 函数获取用户输入的字符串,然后计算字符串长度,并通过循环输出反序后的字符串。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)