BlueJ program on string manipulation. Each words of a sentence will be reversed while the words won't change their positions, only the alphabets in the word will change their locations.
Input: Arav is the best student
Output: varaA si eht tseb tneduts
import java.util.*;
class Search
{
   
String str1,str2="";
   
Scanner sc=new Scanner(System.in);
public void take() 
{    
System.out.print("\nEnter the
sentence:");
str1=sc.nextLine().trim();
System.out.println("\nEntered Sentence:
"+str1);
str1=str1+" ";
while(true)
{
    
int i=str1.indexOf(' ');
    
if(i< 0)
    
break;
    
String s=str1.substring(0,i);
    
str2=str2+" "+rev(s);
    
str1=str1.substring(i+1);
 }
   
str2=str2.trim();
   
System.out.println("\nModified Sentence: "+str2);
}
public String rev(String s)
{
    
String s1="";
    
int len=s.length();
    
for(int i=len-1;i>=0;i--)
    
s1=s1+s.charAt(i);
    
return s1;
}
public static void main(String args[])throws
Exception
{
    
Search ob=new Search();
    
ob.take();
    }
No comments:
Post a Comment