題目描述
Michel需要評審一份文件,但是卻有另一件緊急事情要處理,於是請好友Jack幫忙評審一下。當Machel看到Jack評審結果後,發現一個問題,Jack是以第一人稱評審的,現在Machel需要將Jack評審中第一人(we)和第二人稱(you)進行置換,現在需要請你幫忙完成這個工作
輸入
Jack的評審文字單詞之間是以空格" "或者標準標點符號"," "." "?" "!" ";"作爲分隔符的評審文字內容不超過1024字符長度。
輸出
Machel需要的評審文字
樣例輸入
You are students,you should not get the weapon.
樣例輸出
We are students,we should not get the weapon.
提示
代碼輸入部分參考:
#define MAX_SIZE (1024)
int main()
{
int ncmp = 0;
char szInput[MAX_SIZE * 2];
memset(szInput,0,sizeof(char) * MAX_SIZE * 2);
//! 處理輸入【不能超過MAX_SIZE個字符,否則報錯】
gets(szInput);
if (strlen(szInput) >= MAX_SIZE)
{
printf("Err:Out of range!");
return 0;
}
//! 添加自己業務處理函數
return 0;
};
測試用例參考:
用例1:
輸入:You are students,you should not get the weapon.
輸出:We are students,we should not get the weapon.
地區
公共
產品線
公共
階段
入職前練習
方法一:使用StringTokenizer分割字符串來處理:
public static void wordsReplacementByRegex(String input){
System.out.println(input);
if(input.length()>1024){
System.out.println("Err:Out of range!");
return;//文字內容不超過1024字符長度
}
List<String> list=new ArrayList<String>();
StringTokenizer st = new StringTokenizer(input," ,.?!;",true);
//true:返回時包括分隔符,false:返回時不包括分隔符
while(st.hasMoreTokens()){
list.add(st.nextToken());
}
System.out.println(list);
List<String> liout=new ArrayList<String>();
for(String s:list){
if(s.equals("We")){
s="You";
liout.add(s);
continue;
}
if(s.equals("we")){
s="you";
liout.add(s);
continue;
}
if(s.equals("You")){
s="We";
liout.add(s);
continue;
}
if(s.equals("you")){
s="we";
liout.add(s);
continue;
}
liout.add(s);
}
StringBuilder sb=new StringBuilder();
for(String s:liout){
sb.append(s);
}
System.out.println(sb.toString());
}
方法二:傳統法,字符比較(繁瑣,不建議採用。)
public static void wordsReplacement(String input){
if(input.length()>1024){
System.out.println("Err:Out of range!");
return;//文字內容不超過1024字符長度
}
char[] c=input.toCharArray();//輸入
Stack<Character> stack=new Stack<Character>();
for(int i=c.length-1;i>=0;i--){
stack.add(c[i]);
}
Stack<Character> stack2=new Stack<Character>();
char cPeek=' ';
while(!stack.isEmpty()){
if(!stack2.isEmpty()){
cPeek=stack2.peek();
}
char ch=stack.pop();
if((cPeek==' '||cPeek==','||cPeek=='.'||cPeek=='?'||cPeek==';')&&(ch=='Y'||ch=='y')){
if(!stack.isEmpty()){
if(stack.peek()=='o'){
stack.pop();
if(!stack.isEmpty()){
if(stack.peek()=='u'){
stack.pop();
if(!stack.isEmpty()){
if(stack.peek()==' '){
if(ch=='Y'){
stack2.push('W');
}else{
stack2.push('w');
}
stack2.push('e');
stack2.push(stack.pop());
}else{
char chh=stack.pop();
stack2.push(ch);
stack2.push('o');
stack2.push('u');
stack2.push(chh);
}
}
}else{
char chh=stack.pop();
stack2.push(ch);
stack2.push('o');
stack2.push(chh);
}
}
}
else{
stack2.push(ch);
stack2.push(stack.pop());
}
}
}else{
if((cPeek==' '||cPeek==','||cPeek=='.'||cPeek=='?'||cPeek==';')&&(ch=='W'||ch=='w')){
if(!stack.isEmpty()){
if(stack.peek()=='e'){
stack.pop();
if(!stack.isEmpty()){
if(stack.peek()==' '){
if(ch=='W'){
stack2.push('Y');
}else{
stack2.push('y');
}
stack2.push('o');
stack2.push('u');
stack2.push(stack.pop());
}else{
char chh=stack.pop();
stack2.push(ch);
stack2.push('e');
stack2.push(chh);
}
}
}
else{
stack2.push(ch);
stack2.push(stack.pop());
}
}
}
else{
stack2.push(ch);
}
}
}
stack.clear();
while(!stack2.isEmpty()){
stack.push(stack2.pop());
}
StringBuilder sb=new StringBuilder();
while(!stack.isEmpty()){
sb.append(stack.pop());
}
System.out.println(sb.toString());//輸出
}