40.9k views
0 votes
YOOO CAN ANYONE SOLVE THIS IN JAVA??

YOOO CAN ANYONE SOLVE THIS IN JAVA??-example-1

2 Answers

4 votes

Answer:

I do not know

Step-by-step explanation:

User Sleepless
by
4.3k points
6 votes

public class JavaApplication80 {

public static String swapLetters(String word){

char prevC = '_';

String newWord = "";

int count = 0;

if (word.length() % 2 == 1 || word.isBlank()){

return word;

}

else{

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

char c = word.charAt(i);

if(count % 2 == 1){

newWord += (c +""+ prevC);

}

prevC = c;

count+=1;

}

}

return newWord;

}

public static void main(String[] args) {

System.out.println(swapLetters("peanut"));

}

}

This works for me. Best of luck.

User Wael Chorfan
by
4.3k points