Question:
Does anyone have C++ code to count the occurance of each letter and all puntuation from a file?
thebestgirlintheuniverse
2008-05-18 15:26:03 UTC
It is my last program for this intro C++ class. I have way too much to study. Finals in Physics and Differential Equations and I would LOVE you FOREVER if you could save me the time it would take to write this pgrm.
Thanks soooooo much if you can share your code with me!!! :)

Here is the paragraph I have to use.

All components of an array must be of the same data type; while not extreme this is a serious limitation since there are many situations in which this is not possible. Fortunately, most
programming languages provide another structured data type, structures, which allow heterogeneous information to be stored, accessed, and manipulated. A structure contains fields, which can be of different data types.

so punctuation is just (, . ;)

Thanks agais:)
Three answers:
anonymous
2008-05-19 07:41:03 UTC
Contributor cja has provided a nice solution but probably not one that is going to let you pass your class.



If you are still at a point where you are writing little programs like this and the instructor is still explaining what structure are then it is a safe bet that you haven't covered maps, iterators, etc., in your class. One look at the code and your professor will become highly suspicious about your writing it.



Here is cja's program chopped up a bit to just use arrays. Note that the prof may have wanted you to use a struct with two fields, one for the character and one for the count of that character. That may have been the intent of the lesson but it wasn't necessary to satisfy the requirements of the program.



#include

#include

#include

#include



using namespace std;



int main(int argc, char *argv[])

{

const int ARRAYSIZE = 256;

int array[ARRAYSIZE] = {0};



if (argc != 2)

{

cout << "usage: " << argv[0] << " " << endl;

return(-1);

}



ifstream in(argv[1]);



if (! in)

{

cout << "error opening " << argv[1] << endl;

return(-1);

}



string line;



while (1)

{

getline(in, line);



if (in.eof())

break;



for (int i = 0; i < line.length(); i++)

{

array[line[i]]++;

}

}



cout << "Char : Count" << endl;

cout << "------ -----" << endl;



for (int i = 0; i < ARRAYSIZE; ++i)

{

if (array[i] != 0)

{

cout << " '" << static_cast(i) << "'";

cout << setw(7) << array[i] << endl;

}

}



in.close();



return 0;

}
cja
2008-05-19 01:22:00 UTC
With all due respect to uzman, I believe my code below will work a lot better for you. It counts and displays the frequency of each character in the file specified on the command line. You might be able to make the file I/O better by not having it use a char array. As it is, though, it gets the job done.



#include

#include

#include

#include

#include



using namespace std;



const int MAX_LINE = 256;



int main(int argc, char *argv[]) {

map freq;

map::iterator i;

char line[MAX_LINE];



if (argc > 1) {

ifstream in(string(argv[1]).c_str());

if (in.good() == true) {

while (in.getline(line,MAX_LINE,'\n') != NULL) {

for (unsigned x = 0; x < strlen(line); x++) {

if (freq.find(line[x]) == freq.end()) {

freq.insert(pair(line[x],1));

} else {

freq[line[x]]++;

}

}

}

cout << "Char : Count" << endl;

cout << "------ -----" << endl;

for (i = freq.begin();i != freq.end(); ++i) {

cout << " '" << i->first << "'";

cout << setw(7) << i->second << endl;

}

} else {

cout << "error opening " << argv[1] << endl;

return -1;

}

} else {

cout << "usage: " << argv[0] << " " << endl;

return -1;

}

return 0;

}



#if 0

Sample input file:



All components of an array must be of the same data type; while not

extreme this is a serious limitation since there are many situations

in which this is not possible. Fortunately, most programming languages

provide another structured data type, structures, which allow

heterogeneous information to be stored, accessed, and manipulated.

A structure contains fields, which can be of different data types.



Program output:



Char : Count

------ -----

' ' 59

',' 6

'.' 3

';' 1

'A' 2

'F' 1

'a' 28

'b' 4

'c' 12

'd' 11

'e' 37

'f' 7

'g' 5

'h' 13

'i' 25

'l' 11

'm' 11

'n' 23

'o' 24

'p' 8

'r' 21

's' 27

't' 36

'u' 13

'v' 1

'w' 5

'x' 1

'y' 6



#endif
UZMAN
2008-05-18 23:02:39 UTC
Write in ANSI ISO C++.

#include

#include

#ifdef _WIN32

#include // Microsoft C++ Compiler option

#else

#define _tmain main // UNIX/Linux doesnt support unicode

#define TCHAR char // char macros

#define _TCHAR TCHAR // again

#endif



int _tmain(int, _TCHAR*)

{

using namespace std;

wifstream inputFile( L"inputfile.txt", std::wios::in);

if ( !inputFile )

{

wcout << L"An error occurred when opening inputfile.txt\nPlease Press Return to Continue" << endl;

return wcin.get();

}

size_t count = 0; // Assign 0 for runtime performance

while( inputFile.eof() )

{

wstring temp; // Temp string for counting punc..

getline(inputFile, temp); // Getting the line

// And now we can test if the line contains punctuation char

if ( temp.find( L';' ) != wstring::npos )

count++;// yes line contains char

// Try anoter char:

if ( temp.find( L'.' ) != wstring::npos )

count++;// yes line contains char

}

}



İts all code for me. Good luck.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...