Array-promo

Question

print Words in an odd position in the given sentence.

				
					Enter a sentence
Quick brown fox jumped over the lazy dog
WORDS IN ODD POSITION IN GIVEN SENTENCE
Quick
fox
over
lazy
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class PrintOddPositionWords 
{  
    public static void main(String[] args)
    {  
        String sen="",wd="";
        char ch=' ';
        int i=0,len=0,count=0;
        Scanner sc = new Scanner(System.in);    

        System.out.println("Enter a sentence");   
        sen = sc.nextLine(); 
        sen=sen+" ";
        len=sen.length();
        System.out.println("WORDS IN ODD POSITION IN GIVEN SENTENCE");
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' ')
            {
                count++;
                if(count%2!=0)
                {
                    System.out.println(wd);

                }
                wd="";
            }
            else 
            {
                wd=wd+ch;   
            }
        }
       
    }  
}    


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *