Friday 6 January 2017

Write a program which accept input from user and print the word which has consecutive repeated Character?

Question : Write a program which accept input from user and print the word which has consecutive repeated Character?

Input  :  An apple is banana and banana is apple.

Output : apple,banana,banana,apple

Solution : 
import java.util.*;
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        String str1,str2;
        char ch;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter your string ");
        str1=sc.nextLine();
        int i,start=0,j,flag;
        for(i=1;i<str1.length();i++)
        {
            ch=str1.charAt(i);
            flag=0;
            if(ch==' ')
            {
                str2=str1.substring(start,i);
                
                for(j=0;j<str2.length()-1;j++)
                {
                    if(str2.charAt(j)==str2.charAt(j+1))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                       System.out.println(str2);
                start=i;
            }
                    }
        //for last word
        str2=str1.substring(start,i);
        flag=0;
         for(j=0;j<str2.length()-1;j++)
        {
            if(str2.charAt(j)==str2.charAt(j+1))
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
               System.out.println(str2);

    }
}

Next Program

No comments:

Post a Comment