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
How many types of variables are there in c#?
Why main is static in c#?
Do we get an error while executing the “finally” block in c#?
What is the difference between “dispose” and “finalize” variables in c#?
Why do we need abstract class?
What is the use of parse in c#?
what is IComparable
what is the syntax to inherit from a class in c#?
Why References are stored on heap and variables on stack?
Overloaded constructor will call default constructor internally?
What is the main purpose of linq?
What is data hiding in c#?
How do you read an Excel sheet in C#?
Can we inherit a class with private constructor in c#?
What is public void in c#?