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
Neil ShahAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
you may try the following code:

int Tokenize(const std::string & input,  std::vector<std::string> & realwords)
{
    char seps[] = "ABCDEFGHIJKLMNOPQRSTUVWZabcdefghijklmnopqrstuvwz";
    int  pos       = 0;
    int  lpos      = 0;

    realwords.clear();
    int    len     = input.length(); ;
    while ((pos = input.find_first_not_of(seps, lpos)) >= 0)
    {
        if (pos > lpos)
        {
            // add spaces after real word
            while (pos <= len && input[pos]  == ' ') 
                     pos++;
             realwords.push_back(input.substr(lpos, pos-lpos);
        }
        lpos = pos+1;
    }
    if (lpos < len)
    {
        realwords.push_back(input.substr(lpos));
    }
    return (int)realwords.size();
}

Open in new window


the tokenizer function would search for the next occurrence of any non-alpha, add some trailing spaces if any, and then push the real word to an array.

Sara
1
 
Fabrice LambertFabrice LambertCommented:
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.
1
 
Neil ShahAuthor Commented:
thanks that solved it
0
 
sarabandeCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.