In this Blog I am trying to remove the
duplicates characters. For example the string aabbccdefefe would become
abcdef And in addition to that In this program i am counting vowels and
consonants appeared in a string.
using System;
namespace removing_duplicate_character
{
class Program
{
static void Main(string[] args)
{
int j,i,vowel=0,con=0,pos=1;
string str;
char[] a = new char[30];
Console.WriteLine("Enter
string");
str = Console.ReadLine();
for (i = 0; i < str.Length; i++)
{
a[i] = str[i];
}
for (i = 1; i < str.Length; i++)
{
for (j = 0; j < i; j++)
{
if (a[i] == a[j])
break;
}
if (j == i)
{
a[pos] = a[i];
pos++;
}
}
Console.WriteLine("Output
After Removing Duplicate Character");
for (i = 0; i < pos; i++)
{
Console.Write(a[i]);
}
for (i = 0; i < pos; i++)
{
if (a[i] == 'a' || a[i]
== 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' || a[i] == 'A' ||
a[i] == 'E' || a[i] == 'I' || a[i] == 'O' || a[i] == 'U')
{
vowel++;
}
else
{
con++;
}
}
Console.WriteLine("\nno of
vowels are "+vowel);
Console.WriteLine("no of
consonant are "+con);
Console.ReadLine();
}
}
}


Abrar Ahmad AnsariPosted May 9, 2015, 6:15 AM EDT
using System;using System.Linq; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str; string vowels = "aeiou"; Console.WriteLine("Enter string"); str = Console.ReadLine(); var uniqueChar = str.ToLower().Distinct();//distinct var vowelCount = uniqueChar.Where(c => vowels.Contains(c)).Select(s => s).Count();//find vowels var constant = uniqueChar.Where(c => !vowels.Contains(c)).Select(s => s).Count();//find constant Console.WriteLine("\nno of vowels are " + vowelCount); Console.WriteLine("no of consonant are " + constant); Console.ReadLine(); } } }
Abrar Ahmad AnsariPosted May 9, 2015, 6:15 AM EDT
Why are you writing so long query..try below program without loop..you will get same answer..
Venkatarao DevineniPosted Oct 30, 2014, 2:54 AM EDT
its working good ,But i am unable to understand the logic,,Can u expalin something?
Pramod RawatPosted Sep 25, 2014, 4:00 AM EDT
hi pj explain concept of if(j==i)
Priyanka JainPosted Aug 29, 2013, 12:06 AM EDT
Hii justine . i have done this because i wanted to use a method created by my own instead of using system define method even sometimes in interview they even ask you to create your own method but if you want to use system define method you can use that too.
Justine AliresPosted Aug 28, 2013, 8:43 AM EDT
Hi Priyanka, why did you loop thru the str string and do a value copy character by character to your "a" string in your first for loop instead of using a.str.ToCharArray(). Is it performance?
Priyanka JainPosted Aug 27, 2013, 6:16 AM EDT
i have used 2nd loop for removing duplicate characters only.. and i have executed the program before posting on c# corner and my program is working correctly.. please run the program again.
Jeetendra GundTOP 100Posted Aug 27, 2013, 4:30 AM EDT
but in this program small mistake , i think in writing time..In second for loop of i, it start from 1 here output is wrong bcdef... i corrected to 0 then output is abcdef...Please correct ... Thank u..