package com.pack.string;
import java.util.Scanner;
import java.util.StringTokenizer;
public class WordReplace {
void replace(String str,String rep,String word)
{
StringTokenizer st=new StringTokenizer(str);
int size=st.countTokens();
for(int i=0;i<size;i++)
{
String token=st.nextToken();
if(token.equals(rep))
{
token=word;
}
System.out.print(token+" ");
}
}
public static void main(String[] args) {
System.out.println("Enter the string");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("What to be replaced");
String rep=sc.nextLine();
System.out.println("Replacing word");
String word=sc.nextLine();
WordReplace wr=new WordReplace();
wr.replace(str,rep,word);
}
}
import java.util.Scanner;
import java.util.StringTokenizer;
public class WordReplace {
void replace(String str,String rep,String word)
{
StringTokenizer st=new StringTokenizer(str);
int size=st.countTokens();
for(int i=0;i<size;i++)
{
String token=st.nextToken();
if(token.equals(rep))
{
token=word;
}
System.out.print(token+" ");
}
}
public static void main(String[] args) {
System.out.println("Enter the string");
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
System.out.println("What to be replaced");
String rep=sc.nextLine();
System.out.println("Replacing word");
String word=sc.nextLine();
WordReplace wr=new WordReplace();
wr.replace(str,rep,word);
}
}