Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

can any one tell me best approach to solve this problem.

Suppose I have sentence like,

I/P:"sweet soong suung by richaard"

I need to write a program in c# to remove consecutive vowels(a,e,i,o,u) into single corresponding vowel in each word of the given sentence to bring output like

O/P :"Swet song sung by richard".

What I have tried:

class Program
    {
        
        static void Main(string[] args)
        {
            string inputString = Console.ReadLine();
            string outputString = RemoveVowels(inputString);
            Console.WriteLine(outputString);
            Console.ReadKey();
        }

        static string RemoveVowels(string inputString)
        {
            string tmpStr = "",retStr="";
            string[] splitStr = inputString.Split(Convert.ToChar(" "));
            foreach (var word in splitStr)
            {
                tmpStr = word.Replace("aa", "a").Replace("ee","e").Replace("ii","i").Replace("oo","o").Replace("uu","u");
                if (retStr == "")
                {
                    retStr = tmpStr;
                }
                else
                {
                    retStr += " " + tmpStr;
                }
            }

            return retStr;
        }
    }
Posted
Updated 17-Mar-17 0:23am
Comments
F-ES Sitecore 17-Mar-17 5:45am    
There is no need to split on " " and do each word in turn, just do the replace on the entire string, so "heelloo woorld" would become "hello world". Your current code will probably have problems when there are three letters as "aaa" will become "aa". What I would do is look for instances of "aa" (use IndexOf) and if one is found replace "aa" with "a" and keep doing that until no instance of "aa" can be found. You might also need to think about case issues, so what about "Aare you there?"

However I doubt your tutor wants to you use Replace, this is probably intended as an exercise in using arrays so you'll probably fail the task anyway.
Vijai Anand.G 17-Mar-17 6:52am    
tq..

I'd use a regex:
C#
public static Regex multipleVowels = new Regex("([AEIOUaeiou])\\1{1,}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
public static string regexReplace = "$+";
...
string result = multipleVowels.Replace(InputText,regexReplace);
 
Share this answer
 
Comments
Vijai Anand.G 17-Mar-17 6:53am    
tq..
C#
static string RemoveVowels(string inputString)
    {
      string outoputString = string.Empty;
      char last = '?';
      foreach (char c in inputString)
      {
        if (c == last)
        {
          switch (c)
          {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
              break;
            default:
              outoputString += c;
              break;
          }
        }
        else
          outoputString += c;
        last = c;
      }
      return outoputString;
    }
 
Share this answer
 
Comments
Vijai Anand.G 17-Mar-17 6:59am    
tq..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900