Thursday, February 26, 2015

BlueJ Program On Printing A Sentence Following A Pattern



Write a program to enter a sentence and print in the following pattern:

Entered sentence is: This is a test
output:

This
This is
This is a
This is a test


import java.io.*;
class Sentence
{
 String s1;
 int i,j,len,sp=0,c,s=1;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void take Sentence () throws Exception
 {
  System.out.println("Enter the Sentence:");
  s1=br.readLine();
  len=s1.length()-1;
  for(i=0;i<=len;i++)
  {
   if(s1.charAt(i)==' ')
   s++;
   }
  while(true)
  {
  c=0;
  if (s==sp)
  break;
  sp++;
  for(i=0;i<=len;i++)
  {
  if(s1.charAt(i)==' ')
  c++;
   if(c==sp)
  break;
  System.out.print(s1.charAt(i));
  }
  System.out.println();
   }
  }
   public static void main(String args[])throws Exception
  {
   Sentence ob=new Sentence ();
   ob.take Sentence ();
   }
   }


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner