urgently need help on the C++ question.

Hi All,

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).

Row and Column 5*5.

*
**
***
****
*****
Table Q3(a)(i)

........*
......**
....***
..****
*****
Table Q3(a)(ii)


(b) void extractWord (string& str)

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)

Even Length:

inStr1 = "House Sale";
modified inStr1 = H#u#e#S#l#

Odd Length:
inStr1 = House on Sale
there is no change in inStr1.
Last edited on
And how can we help?
for question (b) use stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
        }
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
		}
	}
}
Last edited on
Thanks Bandit and peamon.

To Bandit , strcat(word,tempStr) what is the command?


To coder777, i need expert to teach me how to solve the above question.
Thanks
For question (a), use O or M to be the decision on which function to use, regular or spaces first.
Use the number for the loop counter.
don't use bandit's code, he is corrupting the heap.

Char pointers are not strings.
For question (c) find length, if even then make a tempStr[], for each letter, copy 1 and then replace 2nd into new tempStr[].
Topic archived. No new replies allowed.