f i give input like 1,1,4,5,6,1,2,5,4,1,2, then output
should be like : 1-4, 2-2, 4-2, 5-1, 6-1 which means 1
occurs 4 times, 2 occurs 2 times like so.
Answer Posted / ganthi ganesh
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GanthiGanesh
{
class Program
{
static void Main(string[] args)
{
int one = 0, two = 0, thr = 0, fou = 0, fiv = 0,
six = 0, sev = 0, eig = 0, nin = 0, zer = 0;
Console.WriteLine("\nEnter here : ");
string ip = Console.ReadLine();
for (int i = 0; i < ip.Length - 1; )
{
char c1 = Convert.ToChar(ip[i]);
i++;
char c2 = Convert.ToChar(ip[i]);
if ((c1 == '0' || c1 == '1' || c1 == '2' ||
c1 == '3' || c1 == '4' || c1 == '5' || c1 == '6' || c1 ==
'7' || c1 == '8' || c1 == '9') && (c2 == ','))
{
i++;
}
else
{
Console.WriteLine("\nFormat error...
Press enter to exit...");
goto a;
}
}
for (int i = 0; i < ip.Length; i++)
{
char c = Convert.ToChar(ip[i]);
if (c == '1')
one++;
else if (c == '2')
two++;
else if (c == '3')
thr++;
else if (c == '4')
fou++;
else if (c == '5')
fiv++;
else if (c == '6')
six++;
else if (c == '7')
sev++;
else if (c == '8')
eig++;
else if (c == '9')
nin++;
else
zer++;
i++;
}
Console.WriteLine("\n");
if (one != 0)
Console.WriteLine("1-" + one.ToString());
if (two != 0)
Console.WriteLine("2-" + two.ToString());
if (thr != 0)
Console.WriteLine("3-" + thr.ToString());
if (fou != 0)
Console.WriteLine("4-" + fou.ToString());
if (fiv != 0)
Console.WriteLine("5-" + fiv.ToString());
if (six != 0)
Console.WriteLine("6-" + six.ToString());
if (sev != 0)
Console.WriteLine("7-" + sev.ToString());
if (eig != 0)
Console.WriteLine("8-" + eig.ToString());
if (nin != 0)
Console.WriteLine("9-" + nin.ToString());
if (zer != 0)
Console.WriteLine("0-" + zer.ToString());
a:
Console.Read();
}
}
}
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
What is the benefit of delegate in c#?
What are the Types of compatabilities and explain them
For methods inside the interface why can’t you specify the accessibility modifier?
Different between method overriding and method overloading?
How do you mark a method obsolete?
What is the use of convert toint32 in c#?
What is xml serialization and deserialization in c#?
What is inumerable?
What is disco?
Explain about Error handling and how this is done
How to exclude a property from xml serialization?
Explain how do I convert a string to an int in c#?
How do I format a string in c#?
What are fields in c#?
What is entity framework in c#?