Find most common Character in string...

I am trying to write a program to find the most common character in a

Page 1 of 1

2 Replies - 17649 Views - Last Post: 03 August 2009 - 11:15 AM Rate Topic: -----

#1 crocadilekyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-August 09

Find most common Character in string...

Posted 02 August 2009 - 08:59 PM

So the program I am working on is suppose to take a user entered string and find the most common character in the string. I am really close.. I think. I'm just not getting the correct output... I think there is a problem in my for loop that is creating the problem. I'm not sure how else to go about this task. Any help is greatly appreciated!

Sorry if this code looks sloppy or not very well done or whatever, I'm new to the game....

#include <iostream>
using namespace std;

void countWords(char *, int);


int main()
{
	const int SIZE = 50;
	char userString[SIZE];
	cout << "Enter a string (up to 50 characters):\n" << endl;
	cin.getline(userString, SIZE);

	countWords(userString, (int) SIZE);

	return 0;
}




void countWords(char *strPtr, int SIZE)
{

int count1;

	for (int count = 0; count < 50; count++)
	{
		if(*(strPtr + count) == *(strPtr + count + count1))
		{
			count1++;
		}
		else
		{
			count++;
		}

	}

	cout << "The mode is: " << *strPtr + count1 << endl;


}




Thanks again for any help....

This post has been edited by crocadilekyle: 02 August 2009 - 09:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Find most common Character in string...

#2 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: Find most common Character in string...

Posted 03 August 2009 - 01:58 AM

#include <iostream>

const int SIZE = 50;

void show_frequent_character(char *, int);

int main()
{
	using namespace std;
	char sequence[SIZE];
	cin.getline(sequence, SIZE);
	
	show_frequent_character(sequence, SIZE);

	return 0;
}

void show_frequent_character(char * characters, int length)
{
	using namespace std;
	char ch;
	int i = 0;
	int max = 0;
	int anothermax = 0;
	int j, counter;
	
	while(characters[i] != '\0')
		i++;
	
	const char * begin;
	const char * end = characters + i;
	
	for(begin = characters; begin != end; begin++)
	{
		j = 0;
		counter = 0;
		
		while(characters[j])
		{
			if( *begin == characters[j])
				counter++;
			j++;
		}
		
		if(counter > max)
		{
			max = counter;
			ch = *begin;
			anothermax = 0;
		}
		else if(counter == max && ch != *begin)
			anothermax++;	 
	
	}
	
	cout << "Max character is " << ch << " which appears " << max << " times.\n";
	
	if(anothermax >0)
		cout << "But there are other characters which appear the same amount of times.\n";
	
 	 
}



Was This Post Helpful? 0
  • +
  • -

#3 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Find most common Character in string...

Posted 03 August 2009 - 11:15 AM

Quote

find the most common character in the string


The easiest way to find the most common character is to create an int array of 255 and just increment the arraly element that corresponds to the character. For example: if the charcter is 'A', then increment the 'A'th element (if you look at any ascii table you will see that the letter 'A' has a decimal value of 65)
int array[255] = {0}; // initialize all elements to 0
char str[] = "The quick brown fox jumped over the lazy dog.";
int i, max, index;
// Now count all the letters in the sentence
for(i = 0; str[i] != 0; i++)
{
   ++array[str[i]];
}
// Find the letter that was used the most 
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
	 if( array[i] > max)
	 {
		 max = array[i];
		 index = i;
	 }
}

cout << "The max character is: " << (char)index << "\n";


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1