Question
Design and implement a C/C++ Program that reads a string (expected to have multiple tokens) from the user and does the following: First: splits the
Design and implement a C/C++ Program that reads a string (expected to have multiple tokens) from the user and does the following:
First: splits the line into tokens based on spaces and (or escape sequences) and stores the tokens in a string vector(myStringTokens) such that each token is stored in a different element. See the examples below:
Command prompt> We call this * a star
We
Call
this
*
a
star
Command prompt> history | grep report > out.txt
history
|
grep
report
>
out.txt
Second: copy the tokens from the vector into a C_String array. (Do some research and ask me if you need assistance). The c-string array must be terminated by NULL. That is, after copying the tokens to the c_string array, make sure that the last element in the c_array contains NULL.
Ex:
char* cmd1[Length+1]; // c_string array declaration
cmd1[length]=NULL;
We
Call
this
*
a
star
NULL
Tips: You need to make sure that the string variables are converted to a c_string when copying them to the c_string array. Read about the method c_str() that is part of the class string in C++ used to converts a CPP string to a c-style string (null-terminated). You also need to read about the explicit char* casting.
Example:
C_stringVariable= (char*) cppStr.c_str(); // string to c_string
Third: implement a boolean function that takes a string variable and your string vector (or another string variable) as arguments and returns true if the string n the first parameter is found in the vector (or the second string variable). The function returns false otherwise.
Note: the function needs to be overloaded (read about that) so that the caller can send one of the following forms:
foo(String, string)
foo(String, string vector)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started