编写一个程序,输入一个字符串,再输入一个字符串,在字符串中删除输入的字符
时间: 2023-12-19 12:07:30 浏览: 77
以下是 Python 代码实现:
```python
string = input("请输入一个字符串:")
char = input("请输入要删除的字符:")
new_string = ""
for c in string:
if c != char:
new_string += c
print("删除后的字符串为:", new_string)
```
首先通过 `input` 函数获取用户输入的两个字符串。然后遍历第一个字符串中的每个字符,如果字符不等于要删除的字符,则将其添加到新的字符串中。最后输出删除后的字符串。
相关问题
c语言编写程序输入一个字符串,统计字符串中字符的个数
可以使用C语言的字符串库函数`strlen()`来统计字符串中字符的个数。以下是一个示例程序:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
printf("该字符串中字符的个数为:%d\n", len);
return 0;
}
```
在程序中,先定义一个字符数组`str`,用于存储输入的字符串。然后使用`scanf()`函数获取用户输入的字符串。接着调用`strlen()`函数获取字符串中字符的个数,并将结果赋给变量`len`。最后,使用`printf()`函数输出结果。
用C语言编写一个程序,输入一个字符串,反转字符串并输出
以下是用C语言编写的程序,输入一个字符串,反转字符串并输出:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i, j;
char temp;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
printf("反转后的字符串:%s", str);
return 0;
}
```
程序首先使用scanf()函数读取用户输入的字符串,并使用strlen()函数获取该字符串的长度。然后,程序使用一个for循环,从字符串的两端开始,依次交换字符,以达到反转字符串的目的。最后,程序输出反转后的字符串。
相关推荐
![](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)