7-4 单词替换 (10分)
设计一个对字符串中的单词查找替换方法,实现对英文字符串中所有待替换单词的查找与替换。
输入格式:
首行输入母字符串,第二行输入查询的单词,第三行输入替换后的单词。
输出格式:
完成查找替换后的完整字符串
输入样例:
在这里给出一组输入。例如:
Although I am without you, I will always be ou you
ou
with
输出样例:
在这里给出相应的输出。例如:
Although I am without you, I will always be with you
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line ,word,replaceword;
int i;
line = scanner.nextLine();
word = scanner.nextLine();
replaceword = scanner.nextLine();
String replaceSentence[] = line.split(" ");
for(i = 0;i<replaceSentence.length;i++){
if(replaceSentence[i].equals(word)){
replaceSentence[i] = replaceword;
}
}
for(i = 0;i<replaceSentence.length-1;i++){
System.out.printf("%s ",replaceSentence[i]);
}
System.out.printf("%s",replaceSentence[i]);
}
}