Link to home
Start Free TrialLog in
Avatar of Neil Shah
Neil Shah

asked on

Help formatting output

Realword: a word that only has letters in it.

Censor Mode: causes the program to only print a word if it is a realword. When in censor mode, any whitespace after the realword is also printed. If a word is not a realword, then it is not printed, nor is any of the whitespace after it.

I am able to check and get rid of the words that digits or punctuations but am not able to take care of the spaces and the newline characters. Below is my code and attached is the input and the expected output and the output that my program is generating.

#include <iostream>
#include <string.h>
#include <fstream>
#include <stdio.h>
#include <cctype>
#include <sstream>
using namespace std;
bool foundRealWord=false;
namespace flagModes{
	string squish (string line){
		string orig = line;
		int len = line.length();
		string squishedString;
		for(int i=0; i<len; i++){
			if(squishedString.length()==0){
				if(isspace(orig[i])){
					continue;
				}
				else{
					squishedString+=orig[i];

				}
			}
			else {
				if(isspace(orig[i])){
					continue;
				}
				else if(isalpha(orig[i])){
					if(isspace(orig[i-1])){
						squishedString+=" ";
						squishedString+=orig[i];
					}
					else{
						squishedString+=orig[i];
					}
				}

			}

	}
		return squishedString;
	}
	string censor (string word){
		string origWord=word;
		string censoredWord="";
		int len = origWord.length();
		if(len==0){
			//censoredWord += "\n";
			return censoredWord;
		}
		else{
			for(int i = 0; i<len; i++){
				if(isalpha(origWord[i])){
					censoredWord+=origWord[i] ;
					foundRealWord=true;
				}
				

				else {
					censoredWord.clear();
					foundRealWord=false;
					break;
				}
			}
		}
		return censoredWord;
	}
}



using namespace flagModes;

ifstream inFile;

int flagCheck(string arg){
	if(arg=="-q"){
		return 1;
	}
	else if(arg == "-s"){
		return 2;
	}
	else if(arg == "-c"){
		return 3;
	}
	else if(arg == "-p"){
		return 4;
	}
	else if(arg == "-i"){
		return 5;
	}
	else if(strpbrk(arg.c_str(),"-")){
		return 6;
	}
	else {
		return 7;
	}
 	return 0;
}


int main(int argc, char* argv[]){
	int fileCount=0 ;

	for(int i = 1; i<argc ; i++){
		switch(flagCheck(argv[i])){
			case 1:
				if((i+1)<argc){
					if(flagCheck(argv[i+1])<=5 && flagCheck(argv[i+1])>1){
						cout << "CONFLICTING FLAGS" << endl;
						exit(1);
					}
					else if(flagCheck(argv[i])==flagCheck(argv[i+1])){
						continue;
					}
					else{
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1] << " CANNOT OPEN" << endl;
							exit(1);
						}
						else{
							fileCount++;
							exit(1);
						}
						inFile.close();
					}
				} exit(1);
			case 2:
				if((i+1)<argc){
					if(flagCheck(argv[i+1])==1){
						cout << "CONFLICTING FLAGS" << endl;
						exit(1);
					}
					else if(flagCheck(argv[i])==flagCheck(argv[i+1])){
						continue;
					}
					else{
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1]<< " CANNOT OPEN" << endl;
							exit(1);
						}
						else{
							fileCount++;
							string line;
							while(getline(inFile , line)){
								if(line.length()!=0){
									cout<<squish(line)<<endl;
								}

							}
						}
						inFile.close();
					}
				} exit(1);
			case 3:
				if((i+1)<argc){
					if(flagCheck(argv[i+1])==1){
						cout << "CONFLICTING FLAGS" << endl;
						exit(1);
					}
					else if(flagCheck(argv[i])==flagCheck(argv[i+1])){
						continue;
					}
					else{
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1]<< " CANNOT OPEN" << endl;
							exit(1);
						}
						else{
							fileCount++;
							string line;
							string word;
							while(getline(inFile, line)){
								istringstream inString(line);
								if(line.length()>0){
									while (inString>>word){

										cout << censor(word)<< " ";

									}
                                 	
								}
								else{
									if(foundRealWord==true){
										;
									}
									continue;
								}
							}
                         	if(inFile.eof()){
                             	cout << endl;
                            }
						}
						inFile.close();
					}
				}
				 exit(1);
			case 4:
				if((i+1)<argc){
					if(flagCheck(argv[i+1])==1){
						cout << "CONFLICTING FLAGS" << endl;
						exit(1);
					}
					else if(flagCheck(argv[i])==flagCheck(argv[i+1])){
						continue;
					}
					else{
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1]<< " CANNOT OPEN" << endl;
							exit(1);
						}
						else{
							fileCount++;

						}
						inFile.close();
					}
				} exit(1);
			case 5:
				if((i+1)<argc){
					if(flagCheck(argv[i+1])==1){
						cout << "CONFLICTING FLAGS" << endl;
						exit(1);
					}
					else if(flagCheck(argv[i])==flagCheck(argv[i+1])){
						continue;
					}
					else{
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1]<< " CANNOT OPEN" << endl;
							exit(1);
						}
						else{
							fileCount++;

						}
						inFile.close();
					}
				} exit(1);
			case 6:
				cout << argv[i] << " INVALID FLAG" << endl;
				exit(1);

			case 7:
				inFile.open(argv[i]);
				if(!inFile){
					cerr << argv[i] << " CANNOT OPEN" << endl;
					exit(1);
				}
				else{
					fileCount++;
					inFile.close();
					if((i+1)<argc){
						inFile.open(argv[i+1]);
						if(!inFile){
							cerr << argv[i+1] << " CANNOT OPEN" << endl;
						}
						else{
							fileCount++;
							inFile.close();
						}
					}
					if(fileCount>1){
						cout << "TOO MANY FILES" << endl;
						exit(0);
					}
					else{
						inFile.open(argv[i]);
						string line;
						while(getline(inFile, line)){
							cout << line << endl;
						}
					}
				}
		}
	}

	return 0;
}

Open in new window

new-3.txt
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe some regex will help:
#include <iostream>
#include <string>
#include <regex>

int main()
{
    const std::string input {"test of real a8nd non real words\nmore to come."};
    std::regex realWord {R"(\b[a-zA-Z]+\b\s*)"};
    for (auto it=std::sregex_iterator(input.begin(), input.end(), realWord); it!=std::sregex_iterator(); ++it)
        std::cout << (*it).str();
    return 0;
}

Open in new window

Additional notes:
Using the directive "using namespace std" is considered a bad practice, its sole purpose is to ensure compatibility with older code base when namespaces were introduced into C++ (back in 1990), it have no business in modern codes.
Either fully qualify your function calls or select precisely what you need, like below:
int main()
{
    std::cout << "Hello world." << std::endl;
}

int main()
{
    using std::cout;
    using std::endl;

    cout << "Hello world." << endl;
}

Open in new window

Pre-declaring your variables is also considered a bad practice (for scope and lifetime reasons), declare them whenever you need them, not before.
Avatar of Neil Shah
Neil Shah

ASKER

thanks that solved it
thanks. i hope, you didn't have issues with the missing bracket at end of the following statement:

realwords.push_back(input.substr(lpos, pos-lpos);   // compile error 

Open in new window


which should have been

realwords.push_back(input.substr(lpos, pos-lpos));

Open in new window


Sara