A string of charaters were given. Find the highest
occurance of a character and display that character.
eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE
Answers were Sorted based on User's Feedback
Answer / paindriven
char HighestCharCount(const char* str)
{
const int length = strlen(str);
if (length == 0)
return ' ';
if (length == 1)
return str[0];
int indexOfHighest = int(-1);
int highestCounter = 0;
for (int i=0; i < length; ++i)
{
const int remain = length - i;
char test = str[i];
int current = 0;
for (int j=i+1; j < remain; ++j)
{
if (str[j] == test)
{
current++;
if (current > highestCounter)
{
highestCounter = current;
indexOfHighest = i;
}
}
}
}
return str[indexOfHighest];
}
Is This Answer Correct ? | 38 Yes | 10 No |
Answer / aman bhullar
#include<string.h>
#include<stdio.h>
char HighestCharCount(char* str)
{
int length = strlen(str);
if (length == 0)
return ' ';
if (length == 1)
return str[0];
int indexOfHighest = -1;
int highestCounter = 0;
int i, j ,current = 0;
char test;
for ( i=0; i < length; ++i)
{
test = str[i];
for (j=i+1; j < length ; ++j)
{
if (str[j] == test)
{
current++;
}
if (current > highestCounter)
{
highestCounter = current;
indexOfHighest = i;
}
}
}
return str[indexOfHighest];
}
void main ( )
{
char a[] = "AEGBCNAvNEETGUPTAEDAGPE";
char res;
res = HighestCharCount(a);
printf("\n %c ", res);
}
Is This Answer Correct ? | 9 Yes | 1 No |
Answer / rohit tehlan
using System;
using System.Linq;
namespace StringHighestCharacterOccur
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the character string ");
string str = Console.ReadLine();
int[] max = new int[str.Length]; // this will store occurence count for each character
for (int i = 0; i < str.Length; i++)
{
int count = 0;
for (int j = 0; j < str.Length; j++)
{
if (str[i] == str[j])
{
count++;
}
}
max[i] = count;
}
int maxvalue = max.Max();
int maxvalueindex = max.ToList().IndexOf(maxvalue); // this will give the index of character with max occurence
Console.WriteLine("Character with max occurence is: " + str[maxvalueindex]);
Console.ReadLine();
}
}
}
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / upinder
to find the answer
visit..
http://advance-programing.blogspot.com/2010/01/string-of-charater-is-givenfind-highest.html
Is This Answer Correct ? | 3 Yes | 1 No |
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class TestClass1 {
public static void main(String[] args) {
HashMap<Character, Integer> hm = new HashMap<Character,
Integer>();
String str = "AABBBCCZX";
if (str != null) {
char[] charArray = str.toCharArray();
for (char ch : charArray) {
int counter = 0;
for (int j = 0; j < charArray.length; j++) {
if (ch == charArray[j]) {
counter = counter + 1;
}
}
hm.put(ch, counter);
}
Set<Character> s = hm.keySet();
Iterator<Character> itr = s.iterator();
while (itr.hasNext()) {
char key = (char) itr.next();
System.out.println("Character : " + key + " Occurence : "
+ hm.get(key));
}
}
}
}
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / guest
using System;
static void main()
{
string a=AEGBCNAvNEETGUPTAEDAGPE;
Is This Answer Correct ? | 2 Yes | 29 No |
what is the software to run the GSM gate opener program
how we define two jobs have same name??is it exist??
smal talk is pure object oriented or not?
Hi guyes, I have cleared 2 technical rounds with cts for liferay and java techonologies, i have client round next week, please tell me how to prepare for this what questions i should be ready to face?
Which method protects back button to retrieve old value from previous page in Struts.
why we use static with only main()class not with other class
what is delegate and delegation model give the real live example on delegate model
Write a function which accepts a sentence as input parameter.Each word in that sentence is to be reversed. Space should be there between each words.Return the sentence with reversed words to main function and produce the required output. for eg:- i/p: jack jill jung kill o/p: kcaj llij gnuj llik
When we have two versions of the dot net installed how does the compiler know which version of DLL it has to select to an application.
3. . Explain the Cache memory? What is the advantage of a processor having more cache memory?
Write code to read the records from a file and load any array of size 99?make sure that you take care of all the error conditions?
Suppose we are doing 4 operations on database using service, first operation is successful but due to some reason remaining 3 operations are failed. I) is this transaction successful or not? ii) How can you give that error message to user?