Infosys Interview Question
Developer Program EngineersCountry: India
Char[] Ch = {'A', 'B', 'A', 'C', 'B','A','C','A','A'};
int a=0,b=0,c=0;
for (int i = 0; i < Ch.Length; i++)
{
switch (Ch[i])
{
case 'A':
a++;
break;
case 'B':
b++;
break;
case 'C':
c++;
break;
}
}
Console.WriteLine("A={0}",a);
Console.WriteLine("B={0}", b);
Console.WriteLine("C={0}", c);
Console.ReadLine();
Java but it requires minor changes to adjust to C
public static void main(String[] args) {
int[] count = new int[128];
char[] input = { 'A', 'B', 'C', 'B', 'A', 'C', 'A', 'B', 'C', 'A', '#' };
for (int i = 0; i < input.length; i++) {
count[input[i]]++;
}
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) {
System.out.println((char) (i) + " = " + count[i]);
}
}
}
There are 256 ASCII characters if you include the extended ones. :P But since this question was asked at Infosys I doubt if the interviewer thought so much.
I'm not sure about C but simply use a HashMap in Java.
- teli.vaibhav January 06, 2014