总结:
考的是字符串的应用。
一开始写错了代码(replace的范围),然后根据输出的情况改了,真不戳。
我的思路:替换。
老师的思路:直接从后往前找到位置修改输出。
AC代码1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
getchar();
while(n--)
{
string a,find1=",";
getline(cin,a);
int temp1=a.find(",",0);
string b;
b.clear();
for(int i=temp1-3;i<=temp1-1;i++)
{
b=b+a[i];
}
if(b!="ong")
{
cout<<"Skipped";
if(n!=0) cout<<endl;
continue;
}
b.clear();
int temp2=a.find(".",0);
for(int i=temp2-3;i<=temp2-1;i++)
{
b=b+a[i];
}
if(b!="ong")
{
cout<<"Skipped";
if(n!=0) cout<<endl;
continue;
}
else
{
//改字
int space=0;
int douhao=a.find(",",0);
for(int i=douhao;a[i];i++)
{
if(a[i]==' ') space++;
}
space=space-2;
int space1=0,temp0=0;
for(int i=douhao;a[i];i++)
{
if(a[i]==' ') space1++;
if(space1==space)
{
temp0=i;
break;
}
}
int l=0;
for(int i=temp0+1;a[i];i++)
{
l++;
}
//temp0是要开始修改的位置
a=a.replace(temp0,l," qiao ben zhong");
cout<<a;
if(n!=0) cout<<endl;
}
}
return 0;
}
AC代码2:
(老师的简洁版)
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[101];
int n;
cin >> n;
int i, j, k;
for(i=0; i<n; i++)
{
getchar();
scanf("%[^\n]", s);
if(strstr(s, "ong,")!=NULL && strstr(s, "ong.")!=NULL)
{
j = strlen(s)-1;
//后除去三个单词
while(s[j]!=' ') j--;
j--;
while(s[j]!=' ') j--;
j--;
while(s[j]!=' ') j--;
for(k=0; k<=j; k++) cout << s[k];
cout << "qiao ben zhong." << endl;
}
else
{
cout << "Skipped" << endl;
}
}
}