Franklin G.
Franklin G.

Reputation: 23

How to add '.' before every consonant in a string?

I'm quite new to Java, I found a question in codeforces.com about deleting all vowels from the string and add dot(.) before every consonant. I already find how to delete all of the vowels, but not yet on how to add dot before every consonant. Please help me :) Thank you

public class Stringprob{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        String word;
        word = s.next();
        word = word.replaceAll("[aeiouyAEIOUY]","");
        //the problem says that 'Y' is a vowel too
        word = insert("[^aeiouyAEIOUY]", ".");
        //here is where I lost it all
        System.out.println(word);
        }
}

Sorry the code is really messy, I'm really new to Java.

Source : String Task - codeforces.com

Upvotes: 1

Views: 5095

Answers (5)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

you can use capture group () and $1 as replacement

1.) Remove all vowels [aeiouyYAEIOU]

2.) Only consonants will be left after applying step 1 so , capture every character using ([a-zA-Z]) and replace it with a .$1 where . is a dot and $1 the character itself

Note : [a-zA-Z] will be more efficient in terms of performance because this will only capture character where as ([^aeiouyAEIOUY]) will match anything other than vowels means it can also match special character and numbers like @#$%^&*12346.

 word  = word .replaceAll("[aeiouyYAEIOU]","");
 word = word.replaceAll("([a-zA-Z])", ".$1"); 

Or to match and add . with special characters and numbers as well

 word  = word .replaceAll("[aeiouyYAEIOU]","");
 word = word.replaceAll("([^aeiouyAEIOUY])", ".$1"); 

([^aeiouyAEIOUY]) : capture all consonants and use replacement as .$1 where $1 represents the captured consonant

[^aeiouyAEIOUY] : ^ mean not in the []

    String s = "abcdefghijklmnopqrstuvwxyzAEIOUY";

    // replace all vowels 
    s = s.replaceAll("[aeiouyYAEIOU]","");
    s = s.replaceAll("([a-zA-z])", ".$1");
    System.out.println(s);

Output :

.b.c.d.f.g.h.j.k.l.m.n.p.q.r.s.t.v.w.x.z

just for your information ,you can also do this in one line that you can apply second replaceAll function on the output of first replaceAll function

s = s.replaceAll("[aeiouyYAEIOU]","").replaceAll("([^aeiouyAEIOU‌​Y])", ".$1");

Upvotes: 4

Sergio David Romero
Sergio David Romero

Reputation: 236

public class Vowels {

    public static boolean isVowel(char input) {
        return (input == 'a' || input == 'e' || input == 'i' || input == 'o'
                || input == 'u' || input == 'A' || input == 'E' || input == 'I'
                || input == 'O' || input == 'U');
    }

    public static String addDots(char input) {

        if (!isVowel(input)) {
            return ".".concat("" + input);
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        String inputString = "Hello";
        String newString = "";
        for (int i = 0; i < inputString.length(); i++) {         
            newString += addDots(inputString.charAt(i));
        }
        System.out.println("Output " + newString);

    }

}

Upvotes: 0

Damian U
Damian U

Reputation: 381

This example add "." before every consonant in a string.

public void test() {
    StringBuffer result = new StringBuffer("");
    String text = "tour";
    StringBuffer b = new StringBuffer(text);
    for(int i = 0; i < b.length(); i++) {
        String substring = b.substring(i, i+1);
        if(isConsanantCharacter(substring)) {
            result.append("." + substring);
        } else {
            result.append(substring);
        }
    }
}

public boolean isConsanantCharacter(String character){
    String cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
    return cons.contains(character);
}

Upvotes: 0

Southc
Southc

Reputation: 23

en,if u can delete all of the vowels,it means the string is only made up of consonants now. pseudocode:

char[] chars=word.toCharArray;
String result;
for(int i;i<chars.length;i++){
    result=result+"."+chars[i];
}

Upvotes: 0

Sourav Purakayastha
Sourav Purakayastha

Reputation: 775

Here's the code:

public static void main(String[] args) {

    String s = "The Boy is a good boy";
    StringBuilder newString = new StringBuilder();

    for (int i = 0; i < s.length(); i++) {
        if (!(s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o'
                || s.charAt(i) == 'u' || s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I'
                || s.charAt(i) == 'U' || s.charAt(i) == 'O' || s.charAt(i) == ' ')) {
            newString.append('.');
        }
        newString.append(s.charAt(i));
    }
    System.out.println(newString);
}

The output is:

.T.he .Bo.y i.s a .goo.d .bo.y

Upvotes: 0

Related Questions