Friday, October 30, 2020

BlueJ Program Printing Consecutive Letters From A Sentence

 
Write a BlueJ program to print consecutive letter from a sentence

import java.util.*;
class Consecutive
{
     String str;
     Scanner sc=new Scanner(System.in);
     char ch;
     int i,len,x=0;
     public void take()
     {
          System.out.print("\nEnter the sentence:");
          str=sc.nextLine();
          len=str.length();
          ch=str.charAt(0);
          System.out.print("\nConsecutive characters in the sentence as follows:");
          for(i=1;i<len;i++)
          {
               if(ch==str.charAt(i))
               {
                 if(x==0)
                 {
                 System.out.print(ch+""+str.charAt(i));
                 x++;
                }
                else
                 System.out.print(ch);
                }
                else// else of  if(ch==str.charAt(i))
                {
                     x=0;
                     ch=str.charAt(i);
                    }
                }
            }
             public static void main(String args[])
             {
                  Consecutive ob=new Consecutive();
                  ob.take();
             }
        }
         

Sample Input Output

 Enter the sentence:This is consecutive letters test

Consecutive characters in the sentence as follows:tt


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner