0% found this document useful (0 votes)
3 views3 pages

Java String Manipulation Programs

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java String Manipulation Programs

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module-3: Strings

1. Write a Java program to reverse each word in a given sentence while maintaining the order
of words.

2. Write a Java program to check if two given strings are anagrams. Two strings are anagrams
if they contain the same characters in the same frequency, but in a different order (ignoring
spaces and case sensitivity).

1. Write a Java program to reverse each word in a given sentence while maintaining the order
of words.

import [Link];
public class M3_1 {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);
[Link]("Enter the sentence : ");
String s1 = [Link]();
String[] s2=[Link](" ");
for(String a:s2) //for each loop
{

StringBuffer sb = new StringBuffer(a);


[Link]();
String rs = [Link]();
[Link](rs+" ");
}

}
}
Output:
2. Write a Java program to check if two given strings are anagrams. Two strings are anagrams
if they contain the same characters in the same frequency, but in a different order (ignoring
spaces and case sensitivity).

import [Link].*;
public class M3_2
{
public static void main(String[] args)
{
//[Link] the strings using scanner class
[Link]("Enter the first string");
Scanner input1=new Scanner([Link]);
String s1=[Link]();
[Link]("Enter the second string");
Scanner input2=new Scanner([Link]);
String s2=[Link]();

//[Link] a Anagrams if Lengths are not equal


if([Link]()!=[Link]())
{
[Link]("Not an Anagrams");
}
else
{
//[Link] the strings into lowercase
String s11=[Link]();
String s22=[Link]();
//[Link] the strings into characters
char[] arr1 = [Link]();
char[] arr2 = [Link]();

// 5. Sort the char array


[Link](arr1);
[Link](arr2);
//[Link] characters into string
String sb1=new String(arr1);
String sb2=new String(arr2);

[Link](sb1);
[Link](sb2);

//[Link] the Equivalence of two strings


if([Link](sb2))

{
[Link]("The given strings are Anagrams");

}
else
{
[Link]("The given strings not Anagrams");

}
}
}
Output:

You might also like