Friday 6 January 2017

Write a program that accept two string one for main string and second string will store some word and remove this word form main string?

Question : Write a program that accept two string one for main string and second string will store some word and remove this word form main string?

Input : ram is a good boy
Search string : ram good
Output : is a boy

Solution : 

import java.util.*;
public class Main
{
  
    public static void main(String[] args)
    {
        int i,start=0,j,flag;
        String str1,str2,str3,str4="";
        char ch;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter your string>> ");
        str1=sc.nextLine();
        str4=str1;
        System.out.print("Enter your Search String>> ");
        str2=sc.nextLine();
        for(i=0;i<str2.length();i++)
        {
            if(str2.charAt(i)==' ')
            {
                str3=str2.substring(start,i);
                str1=str1.replaceAll(str3,"");
                start=i;
            }
        }
        //Getting last word from search string
        str3=str2.substring(start,str2.length());
        str1=str1.replaceAll(str3,"");
        System.out.println("Original string is >> "+str4);
        System.out.println("After remove words string is >> "+str1);
    }
}

Next Question

No comments:

Post a Comment