0

How to i implement loops to print the initialised word as foloows:
Thanx in advance.

p
pr
pro
prog
progr
progra
program
programm
programmi
programmin
programming
programmin
programmi
programm
program
progra
progr
prog
pro
pr
p

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by nmaillet
0

Hint: Use two for loops separately for the increasing pattern and two for decreasing pattern.

Edited by Arbus: n/a

0

So far i have managed the first loop bt im having problem in implementing a decreasing loop.

#include "stdafx.h";
#include  <iostream.h>
#include  "conio.h"

using namespace std;

int main()
{
	    char wrd[12];
		cout<<"\n Enter a String : ";
		cin.getline(wrd,12);
        int i;
		  int j;
		for ( i = 0; i < wrd[i]; i++ )
		{
			for( j=0; j<=i; j++)

             cout<<wrd[j];
		     cout<<"\n";
		}
//=========================================

//    next loop

//=========================================
		 
        cout<<"C++ Programming"<<"\n\n";
		system("pause");
}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

In the previous pattern the letters were increasing and now it should decrease for the next pattern. Just decrement values instead of incrementing. The for loops will look like this.

for(i = strlen(wrd)-1;i>=0;i--)
{ for( j=0;j<=i;j++)
  { 
   //complete this
   .
   .
  }
}

Edited by Arbus: n/a

0

In your first for loop the statement i < wrd[i] should be replaced by either: wrd[i] != NULL or i < strlen(wrd) .

You are comparing i to the character code, which works fine when you are limited to twelve characters, since the lowest printable character is 32 (a space). However, if you went beyond that your code would stop working properly.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.