以下是一个用 C 语言编写的程序,可以实现从键盘输入一个不超过 50 个字符的字符串,将其中的后导 ‘*’ 删除:
时间: 2023-11-27 09:55:24 浏览: 22
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[51];
int len;
printf("请输入一个字符串(不超过50个字符):");
fgets(str, sizeof(str), stdin);
len = strlen(str);
// 删除后导 '*'
while (len > 0 && str[len - 1] == '*') {
str[--len] = '\0';
}
printf("处理后的字符串为:%s\n", str);
return 0;
}
```
注:使用 `fgets` 函数读入字符串可以避免因为输入中包含空格而导致程序读入不完整的情况。同时,由于输入的字符串长度不确定,所以需要使用 `strlen` 函数获取字符串的长度,然后遍历字符串,从后往前删除后导 '*'。
相关问题
从编写程序,实现从键盘输入一个字符串,去重后按字符升序输出。
以下是Python示例代码:
```python
s = input("请输入一个字符串:") # 从键盘输入一个字符串
s = "".join(sorted(set(s))) # 去重并按字符升序排序
print(s) # 输出处理后的字符串
```
说明:
1. `input()`函数用于从键盘输入字符串,将输入的字符串赋值给变量`s`。
2. `set()`函数可以将一个字符串转换为集合,集合中的元素不重复。
3. `sorted()`函数可以对一个可迭代对象进行排序,默认按照字符升序排序。
4. `join()`方法可以将一个可迭代对象中的元素用指定的字符串连接起来,这里用空字符串""连接,从而将集合转换为字符串。
5. 最后输出处理后的字符串。
用python编写程序,实现从键盘输入一个字符串,去重后按字符升序输出
可以使用以下代码实现:
```python
string = input("请输入一个字符串:")
unique_chars = list(set(string))
unique_chars.sort()
print("去重后按字符升序输出:", "".join(unique_chars))
```
这段代码首先使用 `input()` 函数从键盘获取一个字符串,然后使用 `set()` 函数将字符串中的字符去重,再将去重后的字符转换为列表并使用 `sort()` 方法按升序排序。最后使用 `join()` 方法将排序后的字符列表转换为字符串并输出。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)