import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scanner.nextLine();
scanner.close();
String convertedWord = convertWord(word);
System.out.println("Converted word: " + convertedWord);
}
private static String convertWord(String word)
{
StringBuilder converted = new StringBuilder();
for (int i = 0; i < word.length(); i++)
{char convertedChar;
char c = word.charAt(i);
if (c!=' '){
convertedChar = convertCharacter(c);}else{convertedChar =' ';}
converted.append(convertedChar);
}
return converted.toString();
}
private static char convertCharacter(char c)
{
if (isVowel(c))
{
return findNearestConsonant(c);
}
else
{
return findNearestVowel(c);
}
}
private static boolean isVowel(char c)
{
c = Character.toLowerCase(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
private static char findNearestConsonant(char c)
{
while (true)
{
c--;
if (!isVowel(c))
{
return c;
}
}
}
private static char findNearestVowel(char c)
{
while (true)
{
c++;
if (isVowel(c))
{
return c;
}
}
}
}
No comments:
Post a Comment