/* WAP in java to reverse each word of string like
input : "Sriman java group"
output:- "namirS avaJ puorG"
Note: Here instead of String we are using StringBuffer because we cannot modify the string object beacause of String
immutability. If we use String concat() method then every time new object will be created which is memory causes memory
wastage.
*/
class ReverseString2
{
public static void main(String[] alt)
{
String s="Sriman Java Group";
String[] s2=s.split(" ");
StringBuffer sb=new StringBuffer("");
for(String a:s2)
{
for(int i=a.length()-1;i>=0;i--)
sb.append(a.charAt(i));
System.out.print(sb+" ");
sb=new StringBuffer("");
}
}
}
input : "Sriman java group"
output:- "namirS avaJ puorG"
Note: Here instead of String we are using StringBuffer because we cannot modify the string object beacause of String
immutability. If we use String concat() method then every time new object will be created which is memory causes memory
wastage.
*/
class ReverseString2
{
public static void main(String[] alt)
{
String s="Sriman Java Group";
String[] s2=s.split(" ");
StringBuffer sb=new StringBuffer("");
for(String a:s2)
{
for(int i=a.length()-1;i>=0;i--)
sb.append(a.charAt(i));
System.out.print(sb+" ");
sb=new StringBuffer("");
}
}
}
0 Comment to "WAP in java to reverse each word of string like input : "Sriman java group" output:- "namirS avaJ puorG""
Post a Comment