Please include screen shots of code compiling using strings that are provided in question.
All code must be written using C++ programming language.
You will construct a class that defines a parser. This parser will be able to break up a string into tokens. A token is a distinct string of text – it can be a word, a symbol, or a combination of these. Your parser will eliminate whitespace and break string snippets into individual tokens (where single-character tokens are defined at runtime).
Your parser should behave as follows:
1) Whitespace (spaces, tabs, and new lines) is used only to separate tokens – otherwise, it is ignored.
2) Most tokens are made up of strings of text separated punctuation and other symbols.
3) In addition, single-character tokens (punctuation and/or other symbols) may be defined. The parser should treat symbols like any other text character. The default behavior will that all characters not separated by whitespace will be part of the same token. However, the parser will provide several functions that will allow the user to define additional, single-character symbols as token as well.
Target Output:
Input strings ————- [{Oh what a loon() I am! Don’t you agree?}] [I’ll sing “Amazing Grace” for $5. Please call1 the #867-5309.]
Output Delimiting/Parsing by Whitespace only:
—————
[ {Oh what a loon() I am! Don’t you agree?} ] [ I’ll sing “Amazing Grace” for $5. Please call1 the #867-5309. ]
Output Delimiting/Parsing by Block markers:
————–
[ { Oh what a loon ( ) I am! Don’t you agree? } ] [ I’ll sing ” Amazing Grace ” for $5. Please call1 the #867-5309. ]
Output Delimiting/Parsing by Punctuation added:
——————
[ { Oh what a loon ( ) I am ! Don ‘ t you agree ? } ] [ I ‘ ll sing ” Amazing Grace ” for $5 . Please call1 the #867-5309 . ]
Output Delimiting/Parsing by Symbols added:
————–
[ { Oh what a loon ( ) I am ! Don ‘ t you agree ? } ] [ I ‘ ll sing ” Amazing Grace ” for $ 5 . Please call1 the # 867 – 5309 . ]
Screenshot of the code:
Sample output:
Code to copy:
//*********************************************************
//include this header file if you are
// using the visual studio.
#include “stdafx.h”
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
// input string
string input =
” [{Oh what a loon() I am! Don’t you agree?}]”
“[I’ll sing “”Amazing Grace”” for $5.”
“Please call1 the #867-5309.]”;
// declare the global variable.
string token;
vector<string>::iterator iterate;
// method to split the string using the space.
void splitBySpaces1()
{
// create an object of the stringstream.
stringstream str1(input);
cout << “Splitting the string by white spaces n”;
// use while loop print the tokens of the string.
while (getline(str1, token, ‘ ‘))
{
cout << token << endl;
}
}
// Method to split the string using punctuation marks.
void splitByPunctuation()
{
cout << “nSplitting the string by”<<
” punctuation marks n”;
stringstream str2(input);
// Craete a variable of vector type.
vector<string> vect;
// use the while loop to tokenize the string by
// punctuation.
while (std::getline(str2, token))
{
std::size_t temp = 0, pos;
while ((pos = token.find_first_of
(“?.,’;!” “, temp)) != std::string::npos)
{
if (pos > temp)
vect.push_back(token.
substr(temp, pos – temp));
temp = pos + 1;
}
if (temp < token.length())
vect.push_back(token.substr
(temp, std::string::npos));
}
// use for loop to iterarte the vector.
int i = 0;
for (iterate = vect.begin();
iterate < vect.end(); iterate++, i++)
{
cout << *iterate << “n”;
}
}
// function to split the string by symbols.
void splitBySymbol()
{
cout << “nSplitting the string by symbols n”;
int i=0;
stringstream str3(input);
vector<string> vect2;
// use while loop to split the string using
// symbols.
while (std::getline(str3, token))
{
std::size_t temp = 0, pos;
while ((pos = token.find_first_of
(“#@$&~”, temp)) != std::string::npos)
{
if (pos > temp)
vect2.push_back(token.substr
(temp, pos – temp));
temp = pos + 1;
}
if (temp < token.length())
vect2.push_back(token.substr
(temp, std::string::npos));
}
// use for loop to print the tokens.
for (iterate = vect2.begin();
iterate < vect2.end(); iterate++, i++)
{
cout << *iterate << “n”;
}
}
// function to split the string by markers.
void splitByMarker()
{
cout << “nSplitting the string by”<<
” block markers n”;
int i=0;
// craete an object of the vector class.
vector<string> vector2;
stringstream ss4(input);
// use while loop to tokenize the string
// by markers.
while (std::getline(ss4, token))
{
std::size_t previous = 0, pos;
while ((pos = token.find_first_of(“{}”,
previous)) != std::string::npos)
{
if (pos > previous)
vector2.push_back(token.substr
(previous, pos – previous));
previous = pos + 1;
}
if (previous < token.length())
vector2.push_back(token.substr
(previous, std::string::npos));
}
//use for loop print the tokens.
for (iterate = vector2.begin(); iterate
< vector2.end(); iterate++, i++)
{
cout << *iterate << “n”;
}
}
// start the main function of the program.
int main()
{
//call the function to split the string
// by spaces.
splitBySpaces1();
//call the function to split the string
// by punctuation.
splitByPunctuation();
//call the function to split the string
// by symbols.
splitBySymbol();
//call the function to split the string
//by markers..
splitByMarker();
// use this stop the console output.
system(“pause”);
return 0;
}
More Answers
-
As per the give requirement
1. Ask user to enter a Text
2. Ask user enter a Delimiter (devide tokens with this delimiter like punctuation/symbol)
3. Remove all spaces/tabs/newline if they are not delimiters
4. Devide Text by delimiters
Please read comments in code and please comment your feedback
StringTokenizer.h :-
————————————–
#ifndef STRINGTOKENIZER_H
#define STRINGTOKENIZER_H
#include <string>
#include <iostream>
using namespace std;
class StringTokenizer
{
private :
string text;
string delimiter;
public:
StringTokenizer(string text, string delim);
/**
* @brief PrintTokens
* @Desc : This method devides the text into Tokens based on Delimiter (Space/Tab/Symbol...)
*/
void PrintTokens();
/**
* @brief checkValidDelimiter
* @param delim
* @return : True or False
* @Desc : This method checks whether the user enterede delimiter is correct or not
* Delimiter should be space, tab, symbol,panctuation ...
*/
bool checkValidDelimiter(string delim);
/**
* This method is used to remove spaces/tabs/newline if they are not delimiters
*/
void formatText();
/**
*This method is used to remove given sub string from text
*/
void removeSubStr(string substr);
};
#endif // STRINGTOKENIZER_H
StringTokenizer.cpp class :-
————————————–
#include "stringtokenizer.h"
StringTokenizer::StringTokenizer(string text, string delim)
{
this->text = text;
this->delimiter = delim;
}
/**
* @brief PrintTokens
* @Desc : This method devides the text into Tokens based on Delimiter (Space/Tab/Symbol...)
*/
void StringTokenizer::PrintTokens(){
size_t index = 0;
string temp = this->text;
int pos = 0;
while ((pos = temp.find(this->delimiter)) != std::string::npos) {
string token = temp.substr(0, pos);
std::cout << token << std::endl;
if(pos+1 >= temp.length()){
break;
}
temp = temp.substr(pos+1,temp.length()- (pos+2)) ;
}
std::cout << temp << std::endl;
}
/**
* @brief checkValidDelimiter
* @param delim
* @return : True or False
* @Desc : This method checks whether the user enterede delimiter is correct or not
* Delimiter should be space, tab, symbol,panctuation ...
*/
bool StringTokenizer::checkValidDelimiter(string delim){
bool accept = false;
//acdept if Space/Tab
if(delim.compare(" ") == 0 || delim.compare("t") || std::isspace(delim.at(0)) || std::ispunct(delim.at(0))){
accept = true;
}
//Check if not didgit or alpahbet the it should be a symbol
else if(!std::isalpha(delim.at(0)) && std::isdigit(delim.at(0))){
accept = true;
}
return accept;
}
/**
* This method is used to remove spaces/tabs/newline if they are not delimiters
*/
void StringTokenizer::formatText(){
//IF delimiter is not space remove all spaces
if(this->delimiter.compare(" ") != 0 )
removeSubStr(" ");
//IF delimiter is not tab remove all tabs
if(this->delimiter.compare("t") != 0 )
removeSubStr("t");
//IF delimiter is not new line remove all new line cahrs
if(this->delimiter.compare("n") != 0)
removeSubStr("n");
}
/**
*This method is used to remove given sub string from text
*/
void StringTokenizer::removeSubStr(string substr){
size_t start_pos = 0;
while((start_pos = this->text.find(substr, start_pos)) != std::string::npos) {
this->text.replace(start_pos, substr.length(), "");
//start_pos++; // Handles case where 'to' is a substring of 'from'
}
}
main.cpp :-
———————————-
#include <iostream>
#include "stringtokenizer.h"
using namespace std;
int main(int argc, char *argv[])
{
//Take input text from user
cout << "Enter Text" << endl;
string text ;
getline(cin,text);
//Take input delimiter from user
cout << "Enter Delimiter" << endl;
string delim ;
getline(cin,delim);
StringTokenizer tokenizer(text,delim);
if(tokenizer.checkValidDelimiter(delim)){
tokenizer.formatText();
tokenizer.PrintTokens();
}
return 0;
}
Output :-
—————————–