Posted by: cp21 September 24, 2015
C++
Login in to Rate this Post:     0       ?        
Need the program to read txt file with bunch of characters
After reading it should show most commonly used character and least common


#include
#include
#include
#include

using namespace std;

const int SIZE = 50;
char line[SIZE];

void mostFrequent(char *, int);

int main()
{
ifstream inputFile;

//Open the file
inputFile.open("c:/letter_count.txt");
if (inputFile.fail())
{
cout << "Error opening file.\n";
}
else
{
// Process the file.
}

char string[SIZE];
int length;

cin.getline(string, SIZE);
length = strlen(string);
char *stptr = string;
mostFrequent(stptr, length);

cin.get();

return 0;
}

void mostFrequent(char *ptr, int length)
{
int array[256] = { 0 };
int i;
int index = 0;
char ch;

for (i = 0; ptr[i] != '\0'; i++)
{
if (ptr[i] != ' ')
++array[ptr[i]];
}
for (i = 0; i < 256; i++)
cout << array[i];
cout << endl;

int max = array[0];
for (i = 1; i < 256; i++)

{
if (array[i] > max)

{
max = array[i];
index = i;
}
}

cout << "The most common letter occurrences "

<< static_cast(index);

}
Read Full Discussion Thread for this article