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

Recommended Answers

All 4 Replies

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

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");
}

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

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.

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.