Need expert to teach me how to solve the below C++ questions.
Thanks
(a) void Triangle (int, char)
The function takes in two arguments, the integer representing the base and height; and the character representing either original or mirror. These values are use to print the triangle. For e.g. Triangle (5, ‘O’) will display the triangle as shown in Table Q3(a)(i), Triangle (5, ‘M’) will display the triangle as shown in Table Q3(a)(ii).
The function extracts the word between ‘*’. For example, using the three test cases below:
string s = "This is to be *reversed*";
string s1 ="*Reversed* starts here";
string s2 = "This is *in* the middle";
and after each function call,
s = reversed
s1 = Reversed
s2 = in
Note: You may assume that there is only one pair of ‘*’.
(c) void modifiedString (string& inStr1)
The function will perform the following:
If the length of inStr1 is even, it will replace all the even position of the text with ‘#’. If the string is odd, there is no replacement. Note that the spaces between words in the string are included in the length of the line of text. (See examples below)
void extractWord(string& str) {
stringstream ss(str);
stringstream ret;
//if string doesn't start with * go to first one.
if (ss.peek() != '*') {
ss.get(*ret.rdbuf(), '*');
ret.str("");
}
//if stream is good (there is 1 * at least)
if (ss.good()) {
ss.get(); //discard it
if (ss.good()) {
//get content to next *
ss.get(*ret.rdbuf(), '*');
//if stream is good (there is 2 * at least)
if (ss.good()) {
//verifiy there is no other *
stringstream dummy;
ss.get(*dummy.rdbuf(), '*');
if (!dummy.good()) {
//thats ok we update str...
str = ret.str();
}
}
}
}
}
char* extractWord(char *str)
{
for(int i=0;str[i]!='\0';i++) //run through string
{
if(str[i]=='*') // on first occurance of *
{
char tempStr[2],*word="";
i++;
while(str[i]!='*' && str[i]!='\0') // till second * occurs
{
tempStr[0]=str[i]; // as char cannot be used to concat with string
tempStr[1]='\0' // i'm using this tempStr as single char string
strcat(word,tempStr);//append each scanned alphabet to current word
i++;
}
return word;
}
}
}