3.6 字符串处理
3.6 字符串处理
http://codeup.hustoj.com/contest.php?cid=100000580
A 字符串连接

代码
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1,s2;
while(cin>>s1>>s2){
s1+=s2;
cout<<s1<<endl;
}
return 0;
}
好像“无冗余”应该用指针动态开辟空间。【参考https://blog.csdn.net/LJohayao/article/details/88363029】
之前写的错误代码(本地运行没有问题,可能就是因为冗余然后wrong):
#include <cstdio>
#define MAX 250
int main(){
char s[MAX];
char temp;
int num=0;
while(scanf("%c",&temp)!=EOF){
if (temp=='\n'){
s[num]='\0';
printf("%s\n",s);
num=0;
}else if(temp!=' ') s[num++]=temp;
}
return 0;
}
C 字符串的查找删除

题目解析
要注意的是:
- 要不分大小写的删除,但是最后输出要原样输出。意味着你不能把字符串直接都转换成小写,然后进行比对,要保留原来的串。
- 短串不含空格,随便输入。之后输入的若干字符串中可能含有空格,所以要用gets。
- 使用scanf("%s")后用gets,一定要先用getchar()吸收换行。【都用gets就不需要】
解题思路1:
遍历大串b,每个字符b[i] 对照小串a的第一个字符a[0],
- 若不匹配且不是空格,则直接输出b[i] ;
- 若匹配则再进一层循环,大串和小串同时按位比较。若匹配成功则跳过这个部分i+=lena-1(⚠️减1是因为最外层遍历b要i++);若不成功且不是空格,直接输出b[i]
解题思路2:
采用string,直接用find函数进行匹配小串a和空格‘ ’。若匹配上用replace替换为“”或用erase去除。最后输出修改后的大串。
⚠️要不分大小写的删除,但是最后输出要原样输出,所以要保留大串的副本。
代码1
#include <cstdio>
#include <cstring>
#include <cmath>
int main() {
char a[20], b[200];
int lena, lenb;
bool flag;
scanf("%s", a);
lena = strlen(a);
getchar();
while (gets(b) != NULL) {
lenb = strlen(b);
for (int i = 0; i < lenb; i++) {
if (fabs(b[i] - a[0]) == 32 || b[i] - a[0] == 0) { //忽略大小写匹配
flag=true;
for (int j = 1; j < lena; j++) {
if (fabs(b[i + j] - a[j]) != 32 && b[i + j] - a[j] != 0) {
flag = false;
break;
}
}
//b[i]开始匹配a
if (flag) i += lena-1;
else if (b[i] != ' ') printf("%c", b[i]);
}
else if (b[i] != ' ') printf("%c", b[i]);
}
printf("\n");
}
return 0;
}
代码2
#include <iostream>
using namespace std;
int main() {
char tempb[200];
string a, b, c;
cin >> a;
getchar(); //少了getchar!!!!则报格式错误
for (int i = 0; i < a.length(); i++) {
a[i] = tolower(a[i]);
}
while (gets(tempb)!=NULL) {
b = tempb;
c = b;
for (int i = 0; i < c.length(); i++) {
c[i] = tolower(c[i]);
}
int t = c.find(a);
while (t != string::npos) {
b.erase(t,a.length());
c.erase(t,a.length());
//b.replace(t, a.length(), ""); //或用b.erase(t,a.length());
//c.replace(t, a.length(), "");
t = c.find(a);
}
int t2 = c.find(' ');
while (t2 != string::npos) {
b.erase(t2,1);
c.erase(t2,1);
//b.replace(t2, 1, "");
//c.replace(t2, 1, "");
t2 = c.find(' ');
}
cout << b << endl;
}
return 0;
}
D 单词替换

题目解析
与C题思路大同小异:【因为没有说明s、a、b中是否含有空格,一律用gets读入。】
为了看代码方便,s、a、b更新为a、b、c,即此题中要将a中所有b都替换为c输出。
解题思路:
遍历大串a,每个字符a[i] 对照b的第一个字符b[0],
- 若不匹配且不是空格,则直接输出a[i] ;
- 若匹配则再进一层循环,a和b同时按位比较。若匹配成功,则输出c字符串,i+=lenb-1(⚠️减1是因为最外层遍历a要i++);若不成功直接输出a[i]
代码
#include <cstdio>
#include <cstring>
int main(){
char a[105],b[105],c[105];
bool flag;
while(gets(a)!=NULL){
gets(b);
gets(c);
int lena=strlen(a),lenb=strlen(b),lenc=strlen(c);
for (int i=0;i<lena;i++){
flag=true;
if (a[i]==b[0]){
for (int j=1;j<lenb;j++){
if (a[i+j]!=b[j]){
flag=false;
break;
}
}
if (flag){
for (int k=0;k<lenc;k++){
printf("%c",c[k]);
}
i+=lenb-1;
}
else printf("%c",a[i]);
}
else printf("%c",a[i]);
}
printf("\n");
}
return 0;
}
E 字符串去特定字符

题目解析
⚠️题目要循环输入,第二个字符为getchar得到的,为不影响循环后gets读入不必要数据,需要吸收掉换行之类的乱七八糟字符。
故要在while循环中最后加 while((temp=getchar())!='\n' && temp!=EOF);
代码
#include <cstdio>
#include <cstring>
int main(){
char s[1000];
char c;
while(gets(s)){
c=getchar();
for (int i=0;i<strlen(s);i++){
if (s[i]!=c) printf("%c",s[i]);
}
printf("\n");
int temp;
while((temp=getchar())!='\n' && temp!=EOF);
}
return 0;
}
本文作者:Joey-Wang
本文链接:https://www.cnblogs.com/joey-wang/p/14541164.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步