fjxmlhx不喜欢网上的 marshtomps 。所以他决定把所有的 “marshtomp”(名字不区分大小写) 改为 “fjxmlhx”;
Input
Output
Sample Input
Sample Output
输入包含多行,每行字符串不超过200的长度,一个单词不会一半出现在上一行,剩下的在下一行。直到文件结束(EOF)
输出 替换之后的字符串。
The Marshtomp has seen it all before. marshTomp is beaten by fjxmlhx! AmarshtompB
The fjxmlhx has seen it all before. fjxmlhx is beaten by fjxmlhx! AfjxmlhxB
题目看上去很简单,但是有很多处理细节,就是这道题有点难,首先题目输入的字符串是带空格的,而scanf,cin都不会输入空格, gets()可以输入空格,但是和scanf不同,gets()遇到EOF返回NULL,所以循环写成while(gets(s)!=NULL);c++中也给出了输入空格的方法:getline(cin, s)其中s是string类;循环里直接写成while(
getline(cin, s)
)就可以,遇到EOF结束循环;
下面附上代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main(){
string s;
char love[]={'f’,'j','x','m','l','h','x', '\0'};
char hate[]={'m','a','r','s','h','t','o','m','p'};
while(getline(cin, s)){
int i, j;
int len=s.length();
//printf("len:%d\n", len);
int flag[205];
memset(flag, 0, sizeof(flag));
for(i=0; i<len; i++){
for(j=0; j<9; j++){
char a, A;
a=hate[j];
A=hate[j]-'a'+'A';
if(s[i+j]!=a && s[i+j]!=A)
break;
}
if(j>=9){
flag[i]=1;
i+=8;
}
// printf("i:%d j:%d\n", i, j);
}
for(i=0; i<len; i++){
if(flag[i]){
cout << love;
i+=8;
}
else cout << s[i];
}
cout << endl;
}
return 0;
}
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main(){
char s[205];
char love[]={'f','j','x','m','l','h','x', '\0'};
char hate[]={'m','a','r','s','h','t','o','m','p'};
while(gets(s)!=NULL){
int i, j;
int len=strlen(s);
//printf("len:%d\n", len);
int flag[205];
memset(flag, 0, sizeof(flag));
for(i=0; i<len; i++){
for(j=0; j<9; j++){
char a, A;
a=hate[j];
A=hate[j]-'a'+'A';
if(s[i+j]!=a && s[i+j]!=A)
break;
}
if(j>=9){
flag[i]=1;
i+=8;
}
// printf("i:%d j:%d\n", i, j);
}
for(i=0; i<len; i++){
if(flag[i]){
cout << love;
i+=8;
}
else cout << s[i];
}
cout << endl;
}
return 0;
}