The problem is need to rearrange the vowels in the given string in “AEIOU” pattern, below code is accomplish the task.
Example: The given string is KEIOUASAUIOEB
The output is KAAEEISIOOUUB
Example: The given string is KEIOUASAUIOEB
The output is KAAEEISIOOUUB
Here the characters KSB are not vowels so they remains in the same place, reaming vowels are interchanging according to the “AEIOU” pattern.
namespace ArrangeVowels
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the string:");
string inputString = Convert.ToString(Console.ReadLine()).Trim().ToUpper();
int vowelCount = 0;
char swap;
List<int> indexList = new List<int>();
List<char> vowelList = new List<char>();
List<char> outputStringList = new List<char>(inputString);
for (int i = 0; i < inputString.Length; i++)
{
switch (inputString[i])
{
case 'A': vowelCount++; indexList.Add(i); vowelList.Add(inputString[i]); break;
case 'E': vowelCount++; indexList.Add(i); vowelList.Add(inputString[i]); break;
case 'I': vowelCount++; indexList.Add(i); vowelList.Add(inputString[i]); break;
case 'O': vowelCount++; indexList.Add(i); vowelList.Add(inputString[i]); break;
case 'U': vowelCount++; indexList.Add(i); vowelList.Add(inputString[i]); break;
}
}
if (vowelCount > 1)
{
for (int i = 0; i < vowelList.Count; i++)
{
for (int j = 0; j < vowelList.Count - 1; j++)
{
if (vowelList[j] > vowelList[j + 1])
{
swap = vowelList[j+1];
vowelList[j+1] = vowelList[j];
vowelList[j] = swap;
}
}
}
for (int i = 0; i < vowelList.Count; i++)
{
outputStringList[indexList[i]] = vowelList[i];
}
}
Console.WriteLine("The ovwels in string are rearranged in 'AEIOU'");
foreach (char c in outputStringList)
{
Console.Write(c);
}
Console.ReadLine();
}
}
}
