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.

Hello... Try my codes here. It's not that complete but I may teach you how to input right triangle:
*
**
***
****
*****

#include <iostream>
using namespace std;
void main()
{
	int col, row;
	for(row=0;row<5;row++)//row stabds for row
	{
		for(col=0;col<5;col++)//col stands for column
		{
			if (col<=row)
				cout<<'*';
			else
				cout<<" ";
		}
		cout<<endl;
	}
}

I'm not that familiar with strings. I'm sorry.
I hope this code can help you.

Thanks iwishimgoodsau

Hi,
Here is the code for string extraction. As you didn't mention anything about delimiters so I dont know whether you have to use that in the code or not. This code is a version with for loops and if statements.

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std; 

class W_ext{

  public:

  void function()
  {
     char c[100],out[20];
     cout<<"\nEnter the string:";
     cin.getline(c,100);
     
     int j,Flag=1,i=0;

     for(j=0;j<strlen(c);j++)
     {
        if(c[j] == '*' && Flag == 1)
        {
           Flag = Flag * -1;
        }
        else if(Flag==-1 && c[j]!='*')
        {
           out[i] = c[j];
           i++;
        }
        else if(Flag == -1 && c[j]=='*')
        {
            Flag = Flag*-1;
        }


     }

      cout<<out<<endl;
  }



};

int main()
{
  W_ext W;

  W.function();


  return 0;
}

hi software guy, thanks for your help. but question (b) is using string and is pass by reference. I had try to replace char using string but don't seem working(maybe i'm still no good for programming >.<) can you teach me how to use string to answer this question.Thanks

How about you start putting in some effort yourself instead of telling others how to do your homework?

How about you start putting in some effort yourself instead of telling others how to do your homework?

Hello, this is past year examination question that i can't solve. Not my homework okie.
And who said i never put in my effort on solving it? I had be try to solve this questions since past two week. And today i just manage to solve question(c).

string instr1;
	instr1 = "Apple";
	string tempstr="";
	string a;
	string c = "#";

	for (int i =0; i<instr1.length();i++)
	{
		a=instr1[i];

		if(i%2==0)
		{
			tempstr.append(a);
		}
		else
		{
			tempstr.append(c);
		}
	}
	instr1=tempstr;
	cout<<instr1<<endl;
	system ("Pause");
	return 0;
}

Unfortunately polystudent, that doesn't seem to be correct in my opinion. I think you should read the directions again, and actually look at the example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.