The Java Learning Project

I started learning Java programming and found a perfect website for practicing: Codingbat.com. CodingBat is a free site of live coding problems to build coding skill in Java and Python. CodingBat is a project by Nick Parlante, a computer science lecturer at Stanford.

I will upload my solutions here.

Suppose the string "yak" is unlucky. Given a string, return a version where all the "yak" are removed, but the "a" can be any char. The "yak" strings will not overlap.

stringYak("yakpak") → "pak"
stringYak("pakyak") → "pak"
stringYak("yak123ya") → "123ya"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public String stringYak(String str) {
         String result="";
        for (int i=0; i<=str.length()-1; i++){
            if ((i<=str.length()-3) && ((str.substring(i,i+1)).equals("y")) && ((str.substring(i+2,i+3)).equals("k"))) {
                i=i+2;
            }else{
                result=result+str.substring(i,i+1);
            }
        }return result;
}

Keresés

Ajánló