Reverse only the vowels in a string

By   Tewodros   Date Posted: Feb. 28, 2024  Hits: 338   Category:  Algorithm   Total Comment: 0             A+ A-


side

Given a string(word) with a combination of vowels and consonants , we want to reverse just the vowels only with out disturbing the place of the consonants

Example

happen   Ans:  heppan

public string ReverseVowels(string s) {

StringBuilder sb = new StringBuilder(s);

 

char [] v = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

int i=0, j=s.Length-1;

while(i<j)

{

     if(v.Contains(sb[i]))

    {

          if(v.Contains(sb[j]))

         {

              char temp = sb[i];

              sb[i] = sb[j];

              sb[j] = temp;

              i++;j--;

          }

        else

            j--;

     }

   else

      i++;

}

return sb.ToString();

}


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* hdzcoj

Back to Top