Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
static string repvowel(string str)
{
char[] vowels = {'a','e','i','o','u'};
List<char> a1 = vowels.OfType<char>().ToList(); ;
StringBuilder sb = new StringBuilder(str);

for (int i = 0; i < sb.Length; i++)
{
if (a1.Contains(sb[i]))
{
sb.Replace(sb[i].ToString(),"%");
i--;
}
}
return sb.ToString();
}

static string repcons(string cons)
{
char[] word2 = cons.ToCharArray();
for (int j = 0; j < word2.Length; j++)
{
if (word2[j] != 'a' &&
word2[j] != 'e' &&
word2[j] != 'i' &&
word2[j] != 'o' &&
word2[j] != 'u')
{
word2[j] = '#';
}
}
return new string(word2);
}

public static string upper(string cas)
{
char[] word3 = cas.ToCharArray();

for (int l = 0; l < word3.Length; l++)
{
char ch = word3[l];
if (Char.IsLower(ch))
char.ToUpper(ch);
else
char.ToString(ch);
}
return new string(word3);
}


public static void Main()
{
Console.WriteLine("enter first word");
string str = Console.ReadLine();

Console.WriteLine("enter second word");
string cons = Console.ReadLine();

Console.WriteLine("enter third word");
string cas = Console.ReadLine();



Console.Write(repvowel(str));
Console.Write(repcons(cons));
Console.Write(upper(cas));

Console.ReadKey();
}

What I have tried:

static string repvowel(string str)
       {
           char[] vowels = {'a','e','i','o','u'};
           List<char> a1 = vowels.OfType<char>().ToList(); ;
           StringBuilder sb = new StringBuilder(str);

           for (int i = 0; i < sb.Length; i++)
           {
               if (a1.Contains(sb[i]))
               {
                   sb.Replace(sb[i].ToString(),"%");
                   i--;
               }
           }
           return sb.ToString();
       }

       static string repcons(string cons)
       {
           char[] word2 = cons.ToCharArray();
           for (int j = 0; j < word2.Length; j++)
           {
               if (word2[j] != 'a' &&
                   word2[j] != 'e' &&
                   word2[j] != 'i' &&
                   word2[j] != 'o' &&
                   word2[j] != 'u')
               {
                   word2[j] = '#';
               }
           }
           return new string(word2);
       }

       public static string upper(string cas)
       {
           char[] word3 = cas.ToCharArray();

           for (int l = 0; l < word3.Length; l++)
           {
               char ch = word3[l];
               if (Char.IsLower(ch))
                   char.ToUpper(ch);
               else
                   char.ToString(ch);
           }
           return new string(word3);
       }


       public static void Main()
       {
           Console.WriteLine("enter first word");
           string str = Console.ReadLine();

           Console.WriteLine("enter second word");
           string cons = Console.ReadLine();

           Console.WriteLine("enter third word");
           string cas = Console.ReadLine();



           Console.Write(repvowel(str));
           Console.Write(repcons(cons));
           Console.Write(upper(cas));

           Console.ReadKey();
       }
Posted
Updated 7-Apr-20 11:23am

C#
for (int l = 0; l < word3.Length; l++)
{
    char ch = word3[l];
    if (Char.IsLower(ch))
        char.ToUpper(ch);
    else
        char.ToString(ch);
}
return new string(word3);

Look at the code, you convert the character to its upper case equivalent, but you do not save the converted value. And why do you call char.ToString if it is not lower case?
All you need is the something like:

C#
StringBuilder sbUpper = new StringBuilder();
foreach (char ch in word3)
{
    sbUpper.append(ch.ToUpper());
}
return sbUpper.ToString();
 
Share this answer
 
In your code
public static string upper(string cas)

replace this code:
if (Char.IsLower(ch))
char.ToUpper(ch);
else
char.ToString(ch);

with this:
if (Char.IsLower(ch))
word3[l] = char.ToUpper(ch);
 
Share this answer
 
Comments
Richard MacCutchan 6-Apr-20 6:40am    
It is not necessary to test if it is lower case.

You can replace vowels using Linq.

C#
static string ReplaceVowels(string str)
      {
          char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
          var chars = str.Select(c => vowels.Any(v => v == c) ? '%' : c).ToArray();
          return new string(chars);
      }

The same format can be used to replace consonants
C#
public static string ReplaceConsonants(string str)
      {
          char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
          var chars = str.Select(c => !vowels.Contains(c) ? '#' :c ).ToArray();
          return new string(chars);
      }

Coverting a string to uppercase is a function of the string class.
C#
public static string ToUpper(string str)
      {

          return str.ToUpper();
      }

 
Share this answer
 
v3

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