Print Most character Repeated in Console App

Posted by Self-Innovator under C# on 4/12/2012 | Points: 10 | Views : 2189 | Status : [Member] | Replies : 10
Hi frnds...
My qstn:Accept a string from the user and display which character is repeated most. using Console App....
I've tried but strucked.....while printing
Pls rectrify and in my method it return all characters repeated with N times i need the 1 char which occus most times
static void Main(string[] args)
{
string str;
Console.WriteLine("\nEnter the String:");
str= Console.ReadLine();
char[] chars = str.ToCharArray();
List<char> charList = new List<char>();
Console.WriteLine("\nThe entered string is:" + str);
for (int i = 0; i <chars.Length; i++)
{
if (!charList.Contains(chars[i]))
{
charList.Add(chars[i]);
}
}
foreach (char c in charList)
{
int Count = CountChar(str, c);
}
}


private static int CountChar(string Inp, char c)
{
int retVal = 0;
for (int i = 0; i < Inp.Length; i++)
{
if (c == Inp[i])
{
retVal++;
}
}
return retVal;
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Self-Innovator on: 4/12/2012 [Member] Bronze | Points: 25

Up
0
Down
S thanx i'll chck

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Sry its not wrking ....any way tanx for ur resp

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Oh..Nice thought thanks...

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
It throws Excep COnstannts are not allowed...for the Statement

 char tempChar = Constants.vbNullChar;


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
see the Exception....which i've Send Constants are not allowed....

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Sorry Singaravel Still not wrking...pls Chek in ur system once and reply.....

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
See the Error:
Empty character literal
in Statement
 char tempChar = '';


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
SOLVED
 char? tempChar = null;

instead
char tempChar = '';

Any way thanks for ur resp...Singaravel

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Still throws Error here Constants not allowed
  char tempChar = Constants.vbNullChar;


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Wrks fine...
int tempBig = 0;

char? tempChar = null;
//char tempChar = Constants.vbNullChar;
string str = "dotnetfunda";
int tempCount = 0;
foreach (char ch in str)
{
tempCount = 0;
foreach (char ch1 in str)
{
if (ch == ch1)
{
tempCount += 1;
}
}
if (tempBig < tempCount)
{
tempBig = tempCount;
tempChar = ch;
}

}

Console.WriteLine("Char : " + tempChar);
Console.WriteLine("Count : " + tempBig.ToString());
Console.Read();

}

Output

Char:d
Count:2

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response