[Show all top banners]

cp21
Replies to this thread:

More by cp21
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 C++

[Please view other pages to see the rest of the postings. Total posts: 22]
PAGE: <<  1 2  
[VIEWED 10269 TIMES]
SAVE! for ease of future access.
The postings in this thread span 2 pages, go to PAGE 1.

This page is only showing last 20 replies
Posted on 09-24-15 7:31 PM     Reply [Subscribe]
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);

}

 
The postings in this thread span 2 pages, go to PAGE 1.

This page is only showing last 20 replies
Posted on 09-24-15 9:10 PM     [Snapshot: 43]     Reply [Subscribe]
Login in to Rate this Post:     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

}

 
Posted on 09-25-15 7:57 AM     [Snapshot: 107]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

kata gayab ho C++ experts haru, help me with this code its not compiling
 
Posted on 09-25-15 8:55 AM     [Snapshot: 119]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 09-25-15 8:55 AM     [Snapshot: 119]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 



Last edited: 25-Sep-15 08:57 AM

 
Posted on 09-25-15 12:23 PM     [Snapshot: 147]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

toupper A kata cha code ma bro,
and i added entire code and modified few but stilll no go
project due tomorrow cha
 
Posted on 09-25-15 12:28 PM     [Snapshot: 150]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

tyo file in c:/letter_count.txt read bhako nai chaina kina hola
error aihalcha
 
Posted on 09-25-15 1:12 PM     [Snapshot: 168]     Reply [Subscribe]
Login in to Rate this Post:     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?


 
Posted on 09-25-15 1:42 PM     [Snapshot: 180]     Reply [Subscribe]
Login in to Rate this Post:     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;
}


 
Posted on 09-25-15 2:36 PM     [Snapshot: 195]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 09-25-15 2:56 PM     [Snapshot: 221]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thanks for your help alece bro
 
Posted on 09-25-15 3:01 PM     [Snapshot: 227]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

is it good ? can you take it from here now ?

 
Posted on 09-25-15 3:07 PM     [Snapshot: 229]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
Posted on 09-25-15 3:08 PM     [Snapshot: 235]     Reply [Subscribe]
Login in to Rate this Post:     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

 
Posted on 09-25-15 3:14 PM     [Snapshot: 243]     Reply [Subscribe]
Login in to Rate this Post:     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;
}

 
Posted on 09-25-15 3:15 PM     [Snapshot: 246]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 
 
Posted on 09-25-15 3:55 PM     [Snapshot: 263]     Reply [Subscribe]
Login in to Rate this Post:     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!!
 
Posted on 09-25-15 7:24 PM     [Snapshot: 287]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

any thoughts on how to make it work including spaces in text? again i appreciate all your inputs and help

thanks much
 
Posted on 09-28-15 9:26 AM     [Snapshot: 398]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

alece any thoughts and btw after testing your code it shows all characters and does not calc total occurrence
 
Posted on 09-29-15 6:10 PM     [Snapshot: 449]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Does anyone know how to convert c to c++ prg
 
Posted on 10-05-15 8:00 PM     [Snapshot: 566]     Reply [Subscribe]
Login in to Rate this Post:     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
 



PAGE: <<  1 2  
Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration
What are your first memories of when Nepal Television Began?
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
is Rato Bangala school cheating?
Do nepalese really need TPS?
कता जादै छ नेपाली समाज ??
susta manasthiti lai ke bhanchan english ma?
Nas and The Bokas: Coming to a Night Club near you
Will MAGA really start shooting people?
Democrats are so sure Trump will win
मन भित्र को पत्रै पत्र!
Top 10 Anti-vaxxers Who Got Owned by COVID
I regret not marrying a girl at least for green card. do you think TPS will remain for a long time?
काेराेना सङ्क्रमणबाट बच्न Immunity बढाउन के के खाने ?How to increase immunity against COVID - 19?
TPS Work Permit/How long your took?
Breathe in. Breathe out.
Nas and The Bokas: Coming to a Night Club near you
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
is Rato Bangala school cheating?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters