Question

I am trying to write a java method that takes two string parameters from a user...

I am trying to write a java method that takes two string parameters from a user and then returns the first input after removing the letters that appear in the second input.

For example:

if input1 = fortune and input2 = tune

then the method returns "for"

seems simple but I am not understanding exactly how to tackle this

thanks!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Solution

{

    // return true if ch is present in str

    public static boolean find(String str , char ch)

    {

        int i;

       

        // traverse the string

        for( i = 0 ; i < str.length() ; i++ )

        {

            // get character at index i using charAt(i)

            // if the current character is the required character

            if( str.charAt(i) == ch )

                return true;

        }

       

        // if ch is not found in str

        return false;

    }

   

    public static void main(String[] args)

    {

        // create a Scanner class object to get user input

        Scanner sc = new Scanner(System.in);

       

        System.out.print("Enter first string : ");

       

        // read string from user

        String str1 = sc.next();

       

        System.out.print("Enter second string : ");

       

        // read string from user

        String str2 = sc.next();

       

        String ans = "";

        int i;

       

        // traverse the first string

        for( i = 0 ; i < str1.length() ; i++ )

        {

            // get character at index i using charAt(i)

            // if the character at index i is not present in str2

            if( find( str2 , str1.charAt(i) ) == false )

                ans += String.valueOf( str1.charAt(i) );

        }

       

        System.out.println("\nResultant String : " + ans);

    }

}

Sample Output

Add a comment
Know the answer?
Add Answer to:
I am trying to write a java method that takes two string parameters from a user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT