string s;
表示建立一个字符串
他跟c中的char s[1000]一样
但是c语言只能用gets来读取 但是遇到空格就会停止
所以就用c++中的新的函数getline(cin,s);来进行读取 他能读取一整段话
s.length() 就跟c中的strlen()一样 都是用来读取字符串长度的
#include <bits/stdc++.h>
using namespace std;
string s;
int main ()
{
int n;
cin>>n;
getchar();//一定要记得这部操作 因为在n之后你敲了一个回车键 要是不读取的话 就会少读进一句古诗
int i,temp;
for (i=0;i<n;i++){
getline(cin,s);
int j;
for (j=0;j<s.length();j++){
if(s[j]==',') {//古诗中间肯定会有一个逗号 我需要知道逗号在那个位置然后才能进行判断
temp=j;
break;
}
}
int sum=0;
if(s[temp-3]=='o'&&s[temp-2]=='n'&&s[temp-1]=='g'&&s[s.length()-4]=='o'&&s[s.length()-3]=='n'&&s[s.length()-2]=='g'){
for (j=s.length()-1;;j--){
if(s[j]==' '){
sum++;//用sum来记录空格的个数 三个空格就表示已经找到三个词了
if(sum==3){
temp=j;
break;
}
}
}
for(j=0;j<=temp;j++){
cout<<s[j];
}//三个空格前的字都按照原来的样子输出 空格后的就变成敲笨钟
printf("qiao ben zhong.\n");
}
else {
printf("Skipped\n");
}
}
return 0;
}
下方为用c写的程序
#include<stdio.h>
int main()
{
char gushi[100];
int n;
char ch;
scanf("%d",&n);
ch = getchar();
int t = n;
int i,j,q;
while(t--){
int cot=0;
ch = getchar();
for(i=0;ch!='\n';i++) {
gushi[i] = ch;
ch = getchar();
}
for(i=0;;i++){
if(gushi[i]==',')break;
}
if(gushi[i-3]=='o'&&gushi[i-2]=='n'&&gushi[i-1]=='g') {
for(j=i+1;;j++) {
if(gushi[j]=='.') {
gushi[j+1] = '\0';
break;
}
}
if(gushi[j-3]=='o'&&gushi[j-2]=='n'&&gushi[j-1]=='g') {
int len = strlen(gushi);
for(i=len-1;;i--){
if(gushi[i]==' '){
cot++;
}
if(cot==3){
for(q=0;q<=i;q++) {
printf("%c",gushi[q]);
}
printf("qiao ben zhong.\n");
break;
}
}
}else{
printf("Skipped\n");
continue;
}
}
else {
printf("Skipped\n");
continue;
}
}
}