从一个字符串中删除所有同一个给定字符后,得到一个新字符串并输出。如输入字符串:student,and,teacher,输入待删除字符为t,得到的新字符串为:suden,and,eacher。
时间: 2024-02-15 12:02:12 浏览: 25
好的,这是一个字符串操作问题。我会先读取输入字符串和待删除字符,然后遍历字符串逐个字符判断是否为待删除字符,若不是则将该字符加入新字符串中,最后输出新字符串。以下是代码示例:
```python
s = input("请输入字符串:")
c = input("请输入待删除字符:")
new_s = ""
for char in s:
if char != c:
new_s += char
print("新字符串为:", new_s)
```
输出结果会是:
```
请输入字符串:student,and,teacher
请输入待删除字符:t
新字符串为: suden,and,eacher
```
希望能解决你的问题!
相关问题
用c语言来写给定一个字符串,统计其中不同英文字母
的个数,不区分大小写。
以下是代码示例:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int count[26] = {0};
int i, len;
printf("Enter a string: ");
fgets(str, 100, stdin);
len = strlen(str);
for(i = 0; i < len; i++) {
if(isalpha(str[i])) {
str[i] = tolower(str[i]);
count[str[i] - 'a']++;
}
}
printf("The number of different English letters in the given string is: ");
for(i = 0; i < 26; i++) {
if(count[i] > 0) {
printf("%c", i + 'a');
}
}
return 0;
}
对于输入字符串字符串"I am a student, and I like programming!",输出的结果是: adefgiklmnoprrstu。
希望能对你有所帮助!
将字符串中的单词位置倒置,如student a an I倒置后是I am a student
。好的,这是一个关于字符串操作的问题。我可以为您提供一个简单的解决方案:
首先,将给定的字符串按照空格分隔成单词列表。然后,将列表中的单词顺序反转。最后,将反转后的单词列表重新组合成一个字符串。
以下是 Python 代码实现:
```python
s = 'student a an I'
words = s.split() # 将字符串分隔成单词列表
reversed_words = words[::-1] # 反转单词列表
reversed_sentence = ' '.join(reversed_words) # 将反转后的单词列表重新组合成字符串
print(reversed_sentence)
# 输出:I an a student
```
希望这个解决方案对您有帮助!