my program should input a string from the user and output it in the shape of the diamond
if the user enters the word hello, the diamond would print h, then the two e's, then two l's on the next line, two more l's next lines, and two o's on the next line,,
then the bottom half of the diamond would basically be the top half flipped upside down, (not including the last letter), so for example the word "hello" is inputted, there would only be the one middle line of the diamond with the two o's in it
i have code that can do the top half of the diamond, but i dont know how to flip my loops around to do the bottom half
thank you so much anyone that can help
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"enter a word: ";
string word;
getline(cin, word);
int length = word.length();
int z=1;
for ( int i=0; i<=length-1; i++)
{
for (int j=length-1; j>i; j--)
{
cout<<" ";
}
char letter = word[i];
cout<<letter;
if ( i>0)
{
for ( int k=1; k<=z; k++)
{
cout<<" ";
}
z+=2;
char letter = word[i];
cout<<letter;
}
cout<<endl;
}
// can someone explain how to change my loops down here so it will print the bottom half of the diamond???
z=1;
for ( int i=0; i<=length-2; i++)
{
for (int j=length-1; j>i; j--)
{
cout<<" ";
}
char letter = word[i];
cout<<letter;
if ( i>0)
{
for ( int k=1; k<=z; k++)
{
cout<<" ";
}
z+=2;
char letter = word[i];
cout<<letter;
}
cout<<endl;
}
return 0;
}
|