输入一个字符串,将指定的字符从字符串中删除。没有找到要删除的字符,则输出Not found。要求定义函数,并使用指针变量做参数。
时间: 2024-05-31 19:08:36 浏览: 10
#include <stdio.h>
#include <string.h>
void delete_char(char *str, char c) {
size_t len = strlen(str);
int i, j;
for (i = j = 0; i < len; i++) {
if (str[i] != c) {
str[j++] = str[i];
}
}
str[j] = '\0';
if (j == len) {
printf("Not found\n");
}
}
int main() {
char str[100], c;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // remove trailing newline
printf("Enter a character to delete: ");
scanf("%c", &c);
delete_char(str, c);
printf("Result: %s\n", str);
return 0;
}
相关问题
用户输入一个字符串接着再输入一个字符输出该字符串在字符串中所对应的最大下标否则输出“Not Found”
我可以回答你的问题。以下是一个实现输入一个字符串和一个字符,并输出该字符串中对应的最大下标的示例代码:
```
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str;
char ch;
cout << "请输入字符串:" << endl;
getline(cin, str); //获取用户输入的字符串
cout << "请输入一个字符:" << endl;
cin >> ch; //获取用户输入的字符
int maxIndex = -1;
for (int i = 0; i < str.length(); i++) {
if (str[i] == ch) { //查找字符在字符串中出现的位置
maxIndex = i;
}
}
if (maxIndex == -1) {
cout << "Not Found" << endl; //如果没有找到,则输出"Not Found"
} else {
cout << "最大下标为:" << maxIndex << endl;
}
return 0;
}
```
希望能对您有所帮助。
从键盘输入一个字符,在给定的字符串中寻找该字符,若找到打印输出该字符,没找到给提示信息“not found”
可以的,这是一个编程问题,我可以回答。您可以使用字符串的 find() 方法来查找给定字符是否在字符串中,如果找到了,就输出该字符,否则输出“not found”提示信息。以下是一个 Python 代码示例:
```
s = input("请输入字符串:")
c = input("请输入要查找的字符:")
if c in s:
print("找到了该字符:", c)
else:
print("未找到该字符")
```
这个程序会先让用户输入一个字符串和一个要查找的字符,然后使用 in 运算符来判断该字符是否在字符串中,如果在就输出该字符,否则输出“未找到该字符”的提示信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)