Print Most character Repeated in Console App

Posted by Self-Innovator under C# on 4/12/2012 | Points: 10 | Views : 2057 | Status : [Member] | Replies : 23
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: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
code for Print Most character Repeated:

int tempBig = 0;

char tempChar = Constants.vbNullChar;
string str = "Hello";
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();


Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down

u can Understand that code...?

Regards,
Singaravel M

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

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: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
okay

Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
is it working...?

Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/12/2012 [Member] Silver | Points: 25

Up
0
Down
hi self-innovator,

r u got solution for this...

plz reply ur feedback....!!!!!!!!

Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
hello self innovator,
giv ur feedback for ur quries..????

Regards,
Singaravel M

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: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
Thank u for ur feedback, after two days...!!!!!!!!!??????????

Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
in my system its working properly...

wat's the problem..

tell me...

Regards,
Singaravel M

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: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
tell the problem...

its helps to others...


Regards,
Singaravel M

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: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
Replace that...

char tempChar = '';


Regards,
Singaravel M

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: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
in my system its working properly...



Regards,
Singaravel M

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: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
i check it more than 5 systems..

char tempChar = Constants.vbNullChar;


above code not throws any exception...

k...

thank u for ur valuable feedback...

Regards,
Singaravel M

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

Posted by: Sakthi.Singaravel on: 4/13/2012 [Member] Silver | Points: 25

Up
0
Down
thank u for ur valuable feedback...

its may be used for others..

Regards,
Singaravel M

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

Login to post response