C++ - Sajha Mobile
SAJHA MOBILE
C++
Posts 22 · Viewed 10438 · Go to Last Post
cp21
· Snapshot 0
Like · Likedby · 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);

}
Load Previous
cp21
· Snapshot 43
Like · Liked by · 0
modified some but still need your help


#include
#include
#include
#include

using namespace std;

void initialize(int&, int[]);
void copyText(ifstream&, char&, int[]);
void mostCommon(char,int);
void leastCommon(char,int);

int main()
{
int line;
int letter[26];
char ch;
ifstream infile;

infile.open("c:/letter_count.txt");
if (infile.fail())
{
cout << "Error opening file.\n";
}
else
{
// Process the file.
}
initialize(line, letter);
infile.get(ch);

while(infile)
{
copyText (infile,ch,letter);
line++;
infile.get(ch);
}

return 0;
}

void initialize(int& loc, int list[])
{
loc = 0;
for (int i = 0; i < 26; i++)
list[i] = 0;
}

void count(char ch, int list[])
{
ch = toupper(ch);
int index = static_cast(ch)-static_cast('A');
if (0 <= index && index < 26)
list[index]++;
}

void mostCommon(char,int)
{
{
int max = 0;
int count = 0;
string mostCommon;
for (char i = ' '; i <= '~'; i++)

{
count = 0;
for (int i = 0; i {
if (char[i] == i)
count++;
}

if (count == max)
{
mostCommon += i;
}

if (count>max)
{
max = count;
mostCommon = i;
}
}

}

{

cout << "The most common letter occurrences "

<< static_cast

}
cp21
· Snapshot 107
Like · Liked by · 0
kata gayab ho C++ experts haru, help me with this code its not compiling
alece
· Snapshot 119
Like · Liked by · 0
sigh!! kati desperate bhako bro..tait..and start using stackoverflow jeez.
I hope now you can figure out on how to do with file input...

string mostFrequent(string input)
{
int max = 0;
int count = 0;
string mostOccuringChar;
for(char a=' ';a<='~';a++)

{
count = 0;
for(int i=0; i) {
if(input[i]==a)
count++;
}

if(count == max)
{
mostOccuringChar += a;
}

if(count>max)
{
max=count;
mostOccuringChar=a;
}
}

return mostOccuringChar;
}
int main()
{
cout <<"The letter that occurs most is: "<< mostFrequent("Hello World") < system("pause");
}


Last edited: 25-Sep-15 08:58 AM
alece
· Snapshot 119
Like · Liked by · 0


Last edited: 25-Sep-15 08:57 AM
cp21
· Snapshot 147
Like · Liked by · 0
toupper A kata cha code ma bro,
and i added entire code and modified few but stilll no go
project due tomorrow cha
cp21
· Snapshot 150
Like · Liked by · 0
tyo file in c:/letter_count.txt read bhako nai chaina kina hola
error aihalcha
alece
· Snapshot 168
Like · Liked by · 0
File read bhako chaina ? what error are you getting?
Paste you errors.
Here is info on how to read..it's not that hard..
http://www.cplusplus.com/doc/tutorial/files/

toupper kata cha?... why don't u add that?

cp21
· Snapshot 180
Like · Liked by · 0
not sure what am i doing wrong here bro, can you debug and see it for me please


#include
#include
#include
#include

using namespace std;

void initialize(int&, int[]);
void copyText(ifstream&, char&, int[]);
void mostCommon(char,int);
void leastCommon(char,int);

void initialize(int& loc, int list[])
{
loc = 0;
for (int i = 0; i < 26; i++)
list[i] = 0;
}

void count(char ch, int list[])
{
ch = toupper(ch);
int index = static_cast(ch)-static_cast('A');
if (0 <= index && index < 26)
list[index]++;
}

string mostCommon(string input)
{
int max = 0;
int count = 0;
string mostCommonChar;
for (char a = ' '; a <= '~'; a++)

{
count = 0;
for (int i = 0; i;) {
if (input[i] == a)
count++;
}

if (count == max)
{
mostCommonChar += a;
}

if (count > max)
{
max = count;
mostCommonChar = a;

cout << "The letter that occurs most is: " << mostCommon("Hello World") << system("pause");

return mostCommonChar;
}
}


int main()
{
int line;
int letter[26];
char ch;
ifstream infile;

ifstream myfile("c:/letter_count.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
cout << line << '\n';
}
myfile.close();

else cout << "Unable to open file";

}
return 0;
}

alece
· Snapshot 195
Like · Liked by · 0
Here, this works assuming your text file has no spaces between words..
So, modify accordingly.Seriously, can't sit here all day and debug your code.
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string mostFrequent(string input)
{
int max = 0;
int count = 0;
string mostOccuringChar;
for(char a=' ';a<='~';a++)

{
count = 0;
for(int i=0; i) {
if(input[i]==a)
count++;
}

if(count == max)
{
mostOccuringChar += a;
}

if(count>max)
{
max=count;
mostOccuringChar=a;
}
}

return mostOccuringChar;
}


int main()
{
fstream f("C:\\test.txt", fstream::in );
string s;
getline( f, s, '\0');
string test = s;
cout << s << endl;
cout <<"Most frequent is-->"<< mostFrequent(test) <<endl;
f.close();
system("pause");
}
Last edited: 25-Sep-15 02:36 PM
Last edited: 25-Sep-15 02:40 PM
cp21
· Snapshot 221
Like · Liked by · 0
thanks for your help alece bro
alece
· Snapshot 227
Like · Liked by · 0
is it good ? can you take it from here now ?
cp21
· Snapshot 229
Like · Liked by · 0
cp21
· Snapshot 235
Like · Liked by · 0
As you can see its not showing the total number of most commonly used letter
Not sure what is happening, i will have to keep searching
cp21
· Snapshot 243
Like · Liked by · 0
this code actually compiles and shows right result, however i can't use this because it is using C-strings and C-strings functions

I am only allowed to use C++ string class and no global variables are to be used

#include
#include
#include
#include
#include
#include
#include

using namespace std;


class LetterCounter
{
public:
LetterCounter();

void ReadFile(char *lower_count);
void ParseTextAndCountLetters();
void getMinMaxChar(char *minChar, char *maxChar, int *minCount, int *maxCount);

~LetterCounter();


private:
FILE *fpt;
char line[15];

int frequency[26];
string stext;

};


LetterCounter::~LetterCounter()
{
// empty destructor
//
}

LetterCounter::LetterCounter()
{
for (int i = 0; i < 26; i++)
frequency[i] = 0;
//empty constructor

return;
}




void LetterCounter::ReadFile(char *letter_count)
{
errno_t err;
if ((err = fopen_s(&fpt, letter_count, "r")) == 0)
{
printf("\nopenend file %s", letter_count);

while (!feof(fpt))
{
fscanf_s(fpt, "%s", line, _countof(line));
stext.append(line);
}

fclose(fpt);
}
else
{
printf("\nCould not open %s", letter_count);
}

return;
}

void LetterCounter::ParseTextAndCountLetters()
{
int i;
int length = stext.length();


for (i = 0; i < length; i++)
{
char c = toupper(stext.c_str()[i]);

if (c > 64 && c < 92)
{
printf("%c", c);
frequency[c - 65]++;
}

}

return;
}

void LetterCounter::getMinMaxChar(char *minChar, char *maxChar, int *minCount, int *maxCount)
{

*minChar = *maxChar = 0;
*minCount = *maxCount = 0;

*minChar = frequency[0];

for (int i = 0; i < 26; i++)
{
if (frequency[i] > *maxCount)
{
*maxCount = frequency[i];
*maxChar = i + 65;
}

if (frequency[i] < *minCount)
{
*minCount = frequency[i];
*minChar = i + 65;
}


}

}


int main()
{
LetterCounter lc;
ifstream inFile("c:/letter_count.txt", ios::in);

char minChar, maxChar;
int minCount, maxCount;

minChar = maxChar = minCount = maxCount = 0;

lc.ReadFile("c:/letter_count.txt");

lc.ParseTextAndCountLetters();
lc.getMinMaxChar(&minChar, &maxChar, &minCount, &maxCount);

printf("\n The most common letter is %c with = %d occurrences", maxChar, maxCount);



printf("\nPress a key to exit!!");




getchar();

return 0;
}
cp21
· Snapshot 246
Like · Liked by · 0
alece
· Snapshot 263
Like · Liked by · 0
did you even bother to read the comment i put at the top ? i told you it will work if the file has no spaces in it.. sigh!!
cp21
· Snapshot 287
Like · Liked by · 0
any thoughts on how to make it work including spaces in text? again i appreciate all your inputs and help

thanks much
cp21
· Snapshot 398
Like · Liked by · 0
alece any thoughts and btw after testing your code it shows all characters and does not calc total occurrence
cp21
· Snapshot 449
Like · Liked by · 0
Does anyone know how to convert c to c++ prg
hireme
· Snapshot 566
Like · Liked by · 0
There is one opening for c/c++ developer, is there anyone with solid experience on it? I can refer to a company. send me email at findmeitjob@gmail.com
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 4 · Viewed 280
· Posts 6 · Viewed 408
· Posts 8 · Viewed 1264 · Likes 3
· Posts 9 · Viewed 504
· Posts 1 · Viewed 175
· Posts 12 · Viewed 941 · Likes 1
· Posts 1 · Viewed 78
· Posts 1 · Viewed 87
· Posts 9 · Viewed 1092
· Posts 1 · Viewed 123



Your Banner Here
Travel Partners
Travel House Nepal