Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can I iterate through a string to find out each of the character which is constant(not any of these a,e,i,o,u) with out using for or foreach?

please be easy on me if this question does not make any senese.

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 21-May-12 12:00pm    
What do you mean by "constant"? Perhaps you mean "consonant"? :-)
http://en.wikipedia.org/wiki/Consonant

If so, in what language? Just English? What to do with non-letters, letters of different languages?
What's the problem? Just "do my home assignment for me"?
And finally, what do you mean by "be easy on me"? Nobody is going to offend you just because you don't know something, but the help should make some sense to be helpful...

--SA
VJ Reddy 22-May-12 7:56am    
Thank you for accepting the solution :)

Try Linq:
C#
string s = "the quick brown fox";
char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u'};
var res = (from c in s where !(vowels.Contains(c) || c == ' ') select c).Distinct();
 
Share this answer
 
Comments
VJ Reddy 21-May-12 12:14pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 21-May-12 12:20pm    
How about "!"? (non letters?) vowels in different languages?
--SA
Another alternative is to use Regex.Replace method as shown below
C#
string sentence = @"sentence with vowels and consonants";
sentence = Regex.Replace(sentence,@"[aeiou]","*",
    RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Console.WriteLine (sentence);

//Output
//s*nt*nc* w*th v*w*ls *nd c*ns*n*nts


[Edit] Modified to include only consonants in the output [/Edit]

C#
string sentence = @"!sentence with vowels and consonant?";
string onlyConsonants = Regex.Replace(sentence,
     @".*?([ a-z-[aeiou]])|.*?$", "$1",
     RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Console.WriteLine (onlyConsonants);
//output
//sntnc wth vwls nd cnsnnt
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-May-12 12:20pm    
Same notes as for other answers, please see.
--SA
VJ Reddy 21-May-12 12:51pm    
Good point. Modified solution to include only consonants
Thank you.
Sergey Alexandrovich Kryukov 28-May-12 18:30pm    
5ed. Maybe this is still not perfect as it does not address consonant of other languages, but I think it's much more difficult. A number of writing systems don't even have the concept of consonants and vowels. :-)
--SA
VJ Reddy 30-May-12 7:45am    
Thank you very much, SA, for reviewing the comment :)
C#
static void Main(string[] args)
{
    string dataToMatch = "blah blah..AE.good night!@#$~@786%%Gillion!!!";
    string vowel = "aeiouAEIOU";
    Console.WriteLine(new string(dataToMatch.Where(item => !vowel.Contains(item) && char.IsLetter(item)).ToArray()));
}


Hope it helps :)
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 12:13pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 21-May-12 12:19pm    
No, it is not: it would count non-letters (such as "!") as consonants, which is wrong. Checking additionally IsLetter will also yeild wrong result is some other language is involved.
--SA
Mohammad A Rahman 21-May-12 23:54pm    
Thanks for pointing that. I have updated my solution. In regards to the English, as question was asked in English and also does not mention explicitly about the language, I assumed it will just consider English as language(it is my best guess :)) to find out consonants.
VJ Reddy 21-May-12 23:59pm    
Good use of LINQ.
Mohammad A Rahman 22-May-12 0:16am    
Thank you VJ. I kind of addicted with lambda and big fan of curry syntax.
You can use while loop instead of for or foreach.
:)
 
Share this answer
 

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