5
public class ReplaceVowels {

    public static void main(String args[]) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the String:");
        String str = bf.readLine();

        char[] c = new char[str.length()];
        for (int i = 0; i < str.length(); i++) {

            if (c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o'
                    || c[i] == 'u') {

                System.out.println(str.replace(c[i], '?'));

            }

        }

    }
}

why does the str.replace method not work? What should I do to make it work?

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
keith
  • 63
  • 1
  • 1
  • 3

5 Answers5

12

In your code, you're creating a new array of characters, which is the same length as your string, but you're not initialising the array with any values.

Instead, try:

char[] c = str.toCharArray();

However, this isn't the best way of doing what you're trying to do. You don't need a character array or an if statement to replace characters in a string:

String str = bf.readLine();
str.replace( 'a', '?' );
str.replace( 'e', '?' );
str.replace( 'i', '?' );
str.replace( 'o', '?' );
str.replace( 'u', '?' );
System.out.println( str );

The replace function will replace any (and all) characters it finds, or it will do nothing if that character doesn't exist in the string.

You might also want to look into using regular expressions (as pointed out in edwga's answer), so that you can shorten those 5 function calls into one:

str.replaceAll( "[aeiou]", "?" );
Jamie
  • 3,890
  • 3
  • 26
  • 35
  • 2
    Strings are immutable, `str.replace` will not modify original string, it will return new/separate one with modified content. You probably meant `str = str.replace(...)`. – Pshemo Nov 29 '17 at 17:36
5

Honestly, this solution is relatively impractical. You should use the str.replaceAll() method instead.

(read in the String str);
str = str.replaceAll("[aeiou]", "?");
System.out.println(str);

What this does is it uses the regular expression "[aeiou]" and replaces it with a special character ("?"). Regex is a complicated topic, but this one just detects every instance of a vowel. You can read more about Regex at http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html

edwga
  • 190
  • 1
  • 8
2

Here is one way to replace all vowels in a string with an "X". The (?i) is to make it case insensitive.

String str = "hEllo";
str = str.replaceAll( "(?i)[aeiou]", "X" );

Could also be more explicit with case like:

String str = "hEllo";
str = str.replaceAll( "[aeiouAEIOU]", "X" );
Jeff
  • 103
  • 4
1

All the above answers works . Just adding some case sensitivity to capture vowels in upper case(Using Scanner Class)

       String str1, str2;
       Scanner scan = new Scanner(System.in);

       System.out.print("Enter a String : ");
       str1 = scan.nextLine();
       str2 = str1.replaceAll("[aeiouAEIOU]", "?");
       // adding AEIOU to capture Vowels in uppercase.
       System.out.println("All Vowels Removed Successfully");

       System.out.println(str2);
Mohammad Javed
  • 328
  • 3
  • 13
0
/***
 * Replace all vowels in an input string with
 * the corresponding numbers replace with 1,2,3,4,5
 * @author Kishore Diyyana
 */
public class ReplaceVowels {
    public static void main(String args[]) {
        ReplaceVowels rv = new ReplaceVowels();
        System.out.println(rv.replaceVowels("Kishore Babu Diyyana"));
    }
    public String replaceVowels(String inputStr) {
        return inputStr.replaceAll("[aA]","1").
                        replaceAll("[eE]","2").
                        replaceAll("[iI]","3").
                        replaceAll("[oO]","4").
                        replaceAll("[uU]","5");
    }
}