题解分享
题解分享简介
删除字符 - 题解
```
/*
思路分析
思路一:
1. 用char c : str 遍历字符串 ,
2. 比较c 与 ch 并选择输出
思路二:
1. 利用字符串容器特性 删除元素后输出
---remove() 方法可以删除满足条件的字符。 ==>移动到末尾
---erase() 方法可以删除字符串中的指定位置的字符,==> 将末尾的删掉
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin , str);
char ch;
cin >> ch;
for ( char c : str ) {
if ( c != ch ) {
cout << c;
} else {
continue;
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
getline(cin , str);
char ch;
cin >> ch;
//remove()将所有 ch(匹配的字符) 字符移动到字符串的末尾,
//---并返回一个指向“新末尾”的迭代器。
//str.erase(...) 删除从“新末尾”到原末尾的所有字符,
//---从而实现删除指定字符的效果。
str.erase(remove(str.begin() , str.end() , ch), str.end());
for ( char c : str ) {
cout << c;
}
return 0;
}
*/
```
查看全文
0
0
0
1
删除字符 - 题解
```
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char ch=sc.nextLine().charAt(0);
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i)!=ch){
System.out.print(str.charAt(i));
}
}
}
}
```
查看全文
0
0
0
2
删除字符 - 题解
```
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;
string s;
char c;
inline void solve() {
getline(cin,s);
cin >> c;
for (auto &i: s) {
if (i != c)
cout << i ;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
//int _; cin >> _;
while (_--) solve();
return 0;
}
```
```
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;
string s;
char c;
inline void solve() {
getline(cin,s);
cin >> c;
s.erase(remove(s.begin(),s.end(),c), s.end());
cout << s << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
//int _; cin >> _;
while (_--) solve();
return 0;
}
```
查看全文
0
0
0
0
删除字符 - 题解
```c++
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string str;
char ch;
string str2 = "";
getline(cin, str);
cin >> ch;
for (int i = 0; i < str.size(); i++) {
if (str[i] != ch) {
str2 += str[i];
}
}
cout << str2 << endl;
}
```
查看全文
0
0
0
0
删除字符 - 题解
```c++
#include
using namespace std;
int main()
{
string s;
char c;
getline(cin,s);
cin>>c;
int p=s.find(c);
while(p != string::npos)
{
s.erase(p,1);
p= s.find(c);
}
cout<<s;
return 0;
}
```
0
0
0
0



