Computer Science, asked by parulverma03825, 5 months ago

write a program in java to input a sentence in lowercase create a new sentence by replacing each vowel with the next vowel and other character remains the same display the new sentence accordingly​

Answers

Answered by eashanshetty19
1

// Java program of above approach  

class GFG  

{  

// Function to check if a character is  

// vowel or not  

static boolean isVowel(char ch)  

{  

 if (ch != 'a' && ch != 'e' && ch != 'i'

   && ch != 'o' && ch != 'u')  

 {  

  return false;  

 }  

 return true;  

}  

// Function that replaces consonant with  

// next immediate consonant alphabatically  

static String replaceConsonants(char[] s)  

{  

 // Start traversing the string  

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

 {  

  if (!isVowel(s[i]))  

  {  

   // if character is z,  

   // than replace it with character b  

   if (s[i] == 'z')  

   {  

    s[i] = 'b';  

   }  

   

   // if the alphabet is not z  

   else

   {  

    // replace the element with  

    // next immediate alphabet  

    s[i] = (char) (s[i] + 1);  

    // if next immediate alphabet is vowel,  

    // than take next 2nd immediate alphabet  

    // (since no two vowels occurs consecutively  

    // in alphabets) hence no further  

    // checking is required  

    if (isVowel(s[i]))  

    {  

     s[i] = (char) (s[i] + 1);  

    }  

   }  

  }  

 }  

 return String.valueOf(s);  

}  

// Driver code  

public static void main(String[] args)  

{  

 String s = "geeksforgeeks";  

 System.out.println(replaceConsonants(s.toCharArray()));  

}  

}  

Similar questions