Triangle word

How do I get the last section to match the following output form?


Program
Progra
Progr
Prog
Pro
Pr
P

margorP
argorP
rgorP
gorP
orP
rP
P

So far I am able to do the first one, but the second one is where I'm stuck. I am able to get it to print backwards but instead of it removing the first letter it removes the last letter.

Here is my code for it:

[code]
#include <iostream>
#include <string>
using namespace std;

int main() {


string str1;
cout << "Please enter a word: ";
cin >> str1;
int length = str1.length();

cout << endl;
cout << length;

cout << endl;

for (int i = length; i >= 0; i--) {
for (int j = 0; j < i; j++) {
cout << str1[j];
}
cout << endl;
}

cout << endl;
string str2 = string(str1.rbegin(), str1.rend());


for (int i = length; i >= 0; i--) {
for (int j = 0; j < i; j++) {
cout << str2[j];
}
cout << endl;
}


return 0;

}]
@plsalinas

Remember, you've already reversed the word. So why are you now printing from the end?
Try it this way.
1
2
3
4
5
6
7
	for (int i = 0; i < length; i++) {
		for (int j = i; j < length; j++) {//Starting from position i to the end
			cout << str2[j];
		}
		cout << endl;
	}
	return 0;
@whitenite1

I figured it would follow the same logic as my first loop but I see what you mean now. By setting i=0 it's starting it from the beginning instead of the end and it just starting 1 position over each time it loops. Right?

@plsalinas

Yep. That's correct.
Topic archived. No new replies allowed.