18

For example, given a string of Battle of the Vowels:Hawaii vs Gronzy when we specify the characters to be removed as aeiou, the function should transform string to Bttl f th V wls:Hw vs Grzny.

Found this question in the book Programming Interviews Exposed. This was explained in C, however I'm interested in Java.

3
  • 2
    Are you proposing to try to implement this? What have you tried so far? We're not going to do it for you. Commented Feb 12, 2011 at 2:46
  • I was asking for ideas from the community for different ways of implementing it. Commented Feb 12, 2011 at 2:48
  • Great question from future Commented Jul 10, 2023 at 16:59

4 Answers 4

35

One simple way to do this is to use a regular expression:

"Battle of the Vowels:Hawaii vs Gronzy".replaceAll("[aeiou]","")

Some Java Class Library API documentation:

String.replaceAll: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

Patterns and regular expressions: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this: s1 = s1.replaceAll(s2, ""); System.out.println(s1); but it does not work. It seems this solution only works, when we specified the cahractes to be removed.
4
public class RemoveChars {

    char[] replaceChar = {'a','e','i','o','u'};

    public static void main(String[] args) {
        String src = "Battle of the Vowels:Hawaii vs Gronzy";
        System.out.println(new RemoveChars().removeChar(src));
    }

    public String removeChar(String src){
        char[] srcArr = src.toCharArray(); 
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < srcArr.length; i++) {
            char foundChar = isFound(srcArr[i]);
            if(foundChar!='\0')
            sb.append(foundChar);           
        }
        return sb.toString();

    } 

    public char isFound(char src){      
        for (int i = 0; i < replaceChar.length; i++) {
            if(src==replaceChar[i]){
                return '\0';
            }
        }
        return src;
    }
}

Comments

0
public class RemoveCharacters
{
    static String removeCharsFromString(String word1, String word2)
    {
        StringBuilder sb = new StringBuilder(word1);

        System.out.println(sb);
        //char[] word2characters= word2.toCharArray();
        HashMap<Character, Integer> table = new HashMap<Character, Integer>();

        for (int i = 0; i < word2.length(); i++)
        {
            table.put(word2.charAt(i), 1);

        }

        int p = 0;
        for (int i = 0; i < word1.length(); i++)
        {

            if (table.containsKey(word1.charAt(i)))
            {
                if (p == 0)
                {
                    sb.deleteCharAt(i);
                    //p++;
                }
                else
                {
                    sb.deleteCharAt(i - p);
                }
                //System.out.println(sb);
                p++;
            }

        }

        return sb.toString();
    }

    public static void main(String[] args)
    {
        System.out.println("Enter your string");
        Scanner sc = new Scanner(System.in);
        String originalword = sc.nextLine();

        System.out.println("Enter the remove string");
        Scanner sc1 = new Scanner(System.in);
        String removecharacters = sc1.nextLine();

        String result = removeCharsFromString(originalword, removecharacters);

        System.out.println(result);
    }
}

Comments

0
String str = "sandeep";
StringBuilder sb = new StringBuilder();

System.out.println("Enter character to removed: ");
String ch = new Scanner(System.in).next();
for (int i = 0; i < str.length(); i++)
{
    if (str.charAt(i) == ch.charAt(0))
    {
        continue;
    }
    sb.append(str.charAt(i));
}
System.out.println(sb);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.