3
using System;    
using System.Collections.Generic;    
using System.Linq;   
using System.Text;    
using System.Threading.Tasks;

namespace myApp  
{    
    class Program
    {
        static void Main(string[] args)
        {
            string[] vowels = new string[]{"A","a","E","e","I","i","O","o","U","u"};
            for(int j=0;j<vowels.Length;j++)
            {
                string[] names = new string[5];
                names[0] = "john";
                names[1] = "samuel";
                names[2] = "kevin";
                names[3] = "steve";
                names[4] = "martyn";
            for (int i = 0; i < names.Length; i++)
            {
               if(vowels[j]==names[i])
               {

               }
            }


                Console.WriteLine("The output is:"+names[i]);
            }
                Console.ReadLine();
        }
    }
}

can anyone help me how to delete the vowels from the given names and display them in console?

vcsjones
  • 138,677
  • 31
  • 291
  • 286
tom_boy
  • 61
  • 1
  • 8
  • you are comparing the vowels to the entire name. you need to either loop over every letter of each name for your vowel comparison, or use [string.contains](https://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx). Also, you could just store one version of each vowel (instead of upper lower) and handle case in your comparison – devlin carnate Sep 30 '15 at 16:33
  • 1
    change `if(vowels[j]==names[i])` to `names[i].Replace(vowels[j], ' ');` – Les Sep 30 '15 at 16:35
  • You Should ToUpper() the string before you start parsing out vowels and only contain upper case vowels in your string [] vowels. It should improve performance. – PeonProgrammer Sep 30 '15 at 16:36

7 Answers7

2

You can use Linq for it

string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";

var vowels = new HashSet<char>("AaEeIioUu");


names = names.Select(Name => string.Concat(Name.Where(C => !vowels.Contains(C))))
             .ToArray();

Console.WriteLine(string.Join(Environment.NewLine, names));
Eser
  • 12,346
  • 1
  • 22
  • 32
2

You can use Regex.Replace:

Regex r = new Regex("[aAeEiIoOuU]");
//or Regex r = new Regex("[aeiou]", RegexOptions.IgnoreCase);
string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";

for (int i = 0; i < names.Length; i++)
{
    names[i] = r.Replace(names[i], "");

    Console.WriteLine("The output is:" + names[i]);
}

To make your original approach work you need to add a call to string.Replace:

names[i] = names[i].Replace(vowels[j], "");

That says "replace any occurances of vowels[j] in names[i] and assign the result to names[i]".

However, you are currently declaring your array of names inside your vowel loop so you're not going to quite get the result you expect if you add the replacement code.

You're also looping around the vowels and then the names; logically it probably makes sense to reverse this - that certainly makes outputting the results easier. Something like this should work for you:

string[] vowels = new string[] { "A", "a", "E", "e", "I", "i", "O", "o", "U", "u" };

string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";

for (int i = 0; i < names.Length; i++)
{
    for (int j = 0; j < vowels.Length; j++)
    {
        names[i] = names[i].Replace(vowels[j], "");
    }

    Console.WriteLine("The output is:" + names[i]);
}

Edit

In the comments the OP asked for an example without using Replace. Here is one such method (@Eser has another in their answer). This approach iterates each character of the input string until it finds a vowel. At that point the characters that have been read up until then (excluding the vowel) are added to a StringBuilder:

public static string RemoveVowels(string name)
{
    StringBuilder noVowels = new StringBuilder();

    //keep track of the last index we read
    int lastIndex = 0;

    int i;
    for (i = 0; i < name.Length; i++)
    {
        if (vowels.Contains(name[i]))
        {
            //the current index is a vowel, take the text from the last read index to this index
            noVowels.Append(name, lastIndex, i - lastIndex);
            lastIndex = i + 1;
        }
    }

    if (lastIndex < i)
    {
        //the last character wasn't a vowel so we need to add the rest of the string here.
        noVowels.Append(name, lastIndex, name.Length - lastIndex);
    }

    return noVowels.ToString();
}

The above method can be called for each name in your array:

for (int i = 0; i < names.Length; i++)
{
    Console.WriteLine("The output is:" + RemoveVowels(names[i]));
}

As to which approach to use, I would go with the one you find the most readable unless you have some specific performance requirements at which point I think you'd need to measure each approach and pick the one that fits your requirements best.

Community
  • 1
  • 1
petelids
  • 12,305
  • 3
  • 47
  • 57
  • In my humble opinion, using a regular expression for this is a bit overkill. – Andrew Sep 30 '15 at 17:04
  • @j14 - yes, you could use the approach in [Esers answer](http://stackoverflow.com/a/32871559/3791802) or you could iterate each character checking for vowels and add non-vowels to a StringBuilder perhaps (I can provide an example if you'd like). I think any of the approaches (other than the accepted answer) will work for you. – petelids Oct 01 '15 at 12:08
  • @petelids can u answer that fr me..i didnt get that – tom_boy Oct 05 '15 at 09:13
  • @jc54 - I've updated my answer to include an approach. – petelids Oct 05 '15 at 11:45
1

First, you need a nested loop if you are to work on an array of name. You have to loop through your names then inside loop through each vowel. Then use String.Replace to complete the process.

name = name.Replace(vowels[j], String.Empty);
Adam Milecki
  • 1,738
  • 10
  • 15
  • While this is correct, a complete code snippet would greatly improve this answer. – Michael McGriff Sep 30 '15 at 16:57
  • Agreed but I prefer to answer the exact question or fill in the gaps rather than write entire blocks of code that someone can simply copy and paste and not actually learn anything. – Adam Milecki Sep 30 '15 at 17:03
0

I think this is the easiest option:

static void Main(string[] args)
{
    string[] names = new string[] { "john", "samuel", "george", "steve", "martyn" };

    foreach (var name in names)
    {
        string withoutVowels = new string(name.Where(x => !"aeiou".Contains(x)).ToArray());
        Console.WriteLine("The output is: " + withoutVowels);
    }
    Console.ReadKey();
}

If you happen to need it for uppercase too, just use this line instead:

string withoutVowels = new string(name.Where(x => "aeiou".IndexOf(x.ToString(), StringComparison.InvariantCultureIgnoreCase) < 0).ToArray());

Of course, you could also use "aeiouAEIOU" and stick to the first option.

Just for the sake of it, extremely short version, based on Eser's answer:

static void Main(string[] args)
{
    string[] names = new string[] { "johnE", "samuel", "george", "steve", "martyn" };
    Console.WriteLine(string.Join(Environment.NewLine, names.Select(s => new string(s.Where(x => !"aeiou".Contains(x)).ToArray()))));
}
Andrew
  • 7,602
  • 2
  • 34
  • 42
0

Eser's answer is the most succinct and correct way to do this, but in case you want to more fine-grain control over when and which vowels you're removing, you could also try something like:

string[] names = new string[5];
names[0] = "john";
names[1] = "samuel";
names[2] = "kevin";
names[3] = "steve";
names[4] = "martyn";

List<char> vowels = new List<char>("AaEeIiOoUuYy".ToCharArray());

for(int i = 0; i < names.Length; i++) {
    string name = names[i];
    string trimmedName = name;

    foreach(char vowel in vowels) {
        int vowelIndex;
        while((vowelIndex = trimmedName.IndexOf(vowel)) != -1) {
            trimmedName = trimmedName.Substring(0, vowelIndex) + trimmedName.Substring(vowelIndex + 1);
        }

    }
    name = trimmedName;
}

This is a bit more explicit, less performant, and definitely more ugly though -- so you may want to go with the original solution.

funbrigade
  • 321
  • 2
  • 7
-1
forech(string s in strings)
{
    s.Replace("a", "");
    // etc
}
Cjen1
  • 1,826
  • 3
  • 17
  • 47
  • 4
    i didnt downvote but ill tell the reason that its down voted. `Replace` method will return a new string as a result. it will not change a given string. here is `s` which you have to assign it to `s` again to apply changes. hence its not possible with foreach because you cant modify `s` as its readonly value. – M.kazem Akhgary Sep 30 '15 at 16:59
  • Also note that code-only answers are generally not acceptable on StackOverflow, and as such are liable to be deleted. – Wai Ha Lee Oct 01 '15 at 00:24
-1

You could use ToUpper / ToLower to check for the vowels so you don't have to have to list the vowels twice (once for each casing).

First loop through each name, then from each name loop through each vowels. Then, remove the matching vowel with .Replace()

Here is the working example:

fiddle: https://dotnetfiddle.net/STnyWE

using System;    

public class Program
{
  public static void Main()
  {
    string[] vowels = new string[]{"A","E","I","O","U"};


            string[] names = new string[5];
            names[0] = "john";
            names[1] = "samuel";
            names[2] = "kevin";
            names[3] = "steve";
            names[4] = "martyn";

            for (int i = 0; i < names.Length; i++)
            {
                foreach(var v in vowels)
                {

                    if(names[i].ToString().ToUpper().Contains(v.ToString()))
                    {
                        Console.WriteLine(names[i]);
                        names[i] = names[i].ToString().ToUpper().Replace(v.ToString(), "");
                        Console.WriteLine("The output is: "+names[i].ToString().ToLower());
                    }
                }
            }

            Console.ReadLine();
   }
}
NKD
  • 1,039
  • 1
  • 13
  • 24
  • 1
    The problem with this is that you are losing any possible uppercase in the original names. Also, this approach is a bit lengthy, when you can have a much shorter code with Linq, as seen in other answers (mine for example). – Andrew Sep 30 '15 at 17:04
  • I definitely love Linq. I'm a fan. I just wanted to use his existing code and add a little to it to solve the issue. OP didn't have casing in the names from his example so I convert them all to lower. Of course we can use ToTitleCase for the name like CultureInfo.CurrentCulture.TextInfo.ToTitleCase(names[i].ToString().ToLower()) And you're right, if casing happens elsewhere it would be a problem. – NKD Sep 30 '15 at 17:42