Array-promo

Question

program to print reverse of each words in given sentence. 

				
					Enter a sentence
quick brown fox jumped over the lazy dog
given sentence:quick brown fox jumped over the lazy dog 
Reverse of each words:
kciuq   
nworb   
xof   
depmuj   
revo   
eht   
yzal   
god 
				
			

Share code with your friends

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

Code

				
					
import java.util.Scanner;
public class reverseOfEachWordsInSentence 
{  
    public static void main(String[] args)
    {  
        String sen="",wd="";
        char ch=' ';
        int i=0,len=0;
        Scanner sc = new Scanner(System.in);    
        
        System.out.println("Enter a sentence");   
        sen = sc.nextLine(); 
        sen=sen+" ";
        System.out.println("given sentence:"+sen);
        System.out.println("Reverse of each words:");
        len=sen.length();
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' ')
            {
                System.out.println(wd+"   ");
                wd="";
            }
            else
            {
                wd=ch+wd;
                
            }
        }
        
    }  
}    

				
			

Coding Store

Leave a Reply

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