WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
There's the text given (Duomenys.txt). The program removes vowels of the longest words in each line.
How do you redo the program to find numbers in the line (starting with -+0123456789) and insert the word NUMBER before these numbers (in the results file).
const char Cduom[] = "Duomenys.txt"; // data file
const char Crez[] = "Rezultatai.txt"; // results file
const char Canalize[] = "Analize.txt"; // analysis file
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void ApdorotiTeksta(const char dfv[], const char rfv[], const char afv[]); // work text
void AnalizuotiEilute(string eil, string & IlgZodis, int & IlgZPradzia,
unsigned int & IlgZIlgis); // analyse text
void RedaguotiEilute(string &eil, int IlgZPradzia, int IlgZIlgis); // edit line
int main()
{
ApdorotiTeksta(Cduom, Crez, Canalize); // work text
}
//---------------------------------------------------------------------------
// Reading text line by line
// dfv - data file name, rfv - edited text file name
// afv - analysis file name
void ApdorotiTeksta(const char dfv[], const char rfv[], const char afv[]) // work text
{
string IlgZodis; // longest word
int IlgZPradzia; // longest words beginning in the line
unsigned int IlgZIlgis; // length of the longest word
ifstream fd(dfv);
ofstream fr(rfv);
ofstream fa(afv);
string E;
fa << "--------------------------------------\n";
fa << "| Longest word | Beginning | Length |\n";
fa << "--------------------------------------\n";
while(!fd.eof()) {
getline(fd, E);
AnalizuotiEilute(E, IlgZodis, IlgZPradzia, IlgZIlgis); // analyse line
if(IlgZIlgis > 0){
fa << "| " << left << setw(16) << IlgZodis << " | "
<< right << setw(7) << IlgZPradzia;
fa << " | " << setw(5) << IlgZIlgis << " |\n";
RedaguotiEilute(E, IlgZPradzia, IlgZIlgis); // edit line
}
fr << E << endl;
}
fa << "--------------------------------------\n";
fd.close();
fr.close();
fa.close();
}
//---------------------------------------------------------------------------
// Function which finds the longest word in the line, the beginning of that word and the length
// eil - line where the search is being done
// IlgZodis - returns the longest word in the line
// IlgZPradzia - returns the beginning ot the word
// IlgZIlgis - returns the length of the word
void AnalizuotiEilute(string eil, string & IlgZodis, int & IlgZPradzia, unsigned int & IlgZIlgis) // analyse line
{
string Skirt = " .,!?:;()\t"; // marks between words
string Zodis; // word
int zpr = 0, zpb = 0;
IlgZodis = "";
IlgZPradzia = 0; IlgZIlgis = 0;
while ((zpr = eil.find_first_not_of(Skirt, zpb)) != string::npos) {
zpb = eil.find_first_of(Skirt, zpr);
Zodis = eil.substr(zpr, zpb - zpr);
if (Zodis.length() > IlgZIlgis) {
IlgZodis = Zodis;
IlgZPradzia = zpr;
IlgZIlgis = Zodis.length();
}
}
}
//---------------------------------------------------------------------------
// Function which removes vowels from the longest word
// eil - line which is exchanged
// IlgZPradzia - beginning of the longest word in the line
// IlgZIlgis - length of the longest word in the line
void RedaguotiEilute(string &eil, int IlgZPradzia, int IlgZIlgis) // edit line
{
string Balses = "AEIYOUaeiyou"; // vowels
int i = IlgZPradzia;
while (i < IlgZPradzia + IlgZIlgis)
if (Balses.find_first_of(eil[i]) != string::npos) {
eil.erase(i, 1);
IlgZIlgis--;
}
else
i++;
}
//----------------------------------------------------------How do you redo the program to find numbers in the line (starting with -+0123456789) and insert the word NUMBER before these numbers (in the results file).
Find each word
Test the first character
If -/+/digit then output the word NUMBER
Output the word
Repeat.