Q1)Print the first unique character from a given string.
input as: abbacdeab
Output as: c
Ans:
public class MainClass{
public static char printFirstUniqueCharacters(String s){
char[] ch = [Link]();
for(int i=0; i<[Link]; i++) {
int j=0;
for(; j<[Link]; j++) if(i!=j && ch[i]==ch[j]) break;
if(j == [Link]) return ch[i];
}
return '-';
}
public static void main(String []args){
String s = "Developer";
[Link]("First unique character: " +
printFirstUniqueCharacters(s));
}
}
===================================================================================
================
Q2)Check the String is Anagram or not.
String s1 = "care";
String s2 = "race";
Output : Strings are anagram
Ans:
import [Link];
public class MainClass{
public static boolean isAnagram(String s1, String s2){
if([Link]() == [Link]()){
char []c1 = [Link]();
char []c2 = [Link]();
[Link](c1);
[Link](c2);
int i=0;
for(; i<[Link]; i++) if(c1[i]!=c2[i]) return false;
if(i == [Link]) return true;
}
return false;
}
public static void main(String []args){
String s1 = "care";
String s2 = "race";
[Link]("Strings is anagram: " + isAnagram(s1, s2));
}
}
===================================================================================
================
Q3)Reverse each word of a given sentence at their place.
Input as : java is a platform
Output as: avaj si a mroftalp
Ans:
public class MainClass{
public static String reverseEachWord(String s){
String []sa = [Link]("\\s");
String res = "";
for(int i=0; i<[Link]; i++){
char []ch = sa[i].toCharArray();
for(int j=0; j<[Link]/2; j++){
char t = ch[j];
ch[j] = ch[[Link]-j-1];
ch[[Link]-j-1] = t;
}
res += " " + [Link](ch);
}
return res;
}
public static void main(String []args){
String s = "java is a platform";
[Link]("After reversed each word: " + reverseEachWord(s));
}
}
===================================================================================
================
Q4) Find the longest palindrome in a given String.
Input as : nitin is learning malayalam from madam liril.
Output as: Malayalam
Ans:
public class MainClass{
public static String findLargestPalindrome(String s){
int max = 0;
String largestPalindeome = "";
String []sa = [Link]("\\s");
for(int i=0; i<[Link]; i++){
int j=0;
char []t = sa[i].toCharArray();
for(; j<[Link]/2; j++) if(t[j] != t[[Link]-j-1]) break;
if(j == [Link]/2) if(max < [Link]){
max = [Link];
largestPalindeome = sa[i];
}
}
return largestPalindeome;
}
public static void main(String []args){
String s = "nitin is learning malayalam from madam liril";
[Link]("Largest palindrome: " + findLargestPalindrome(s));
}
}
===================================================================================
================
Q5) Find the digit sum from the given String.
Input as: @g123mai5l1.c2om
Output as: 14
Ans:
public class MainClass{
public static int sumOfDigitsInString(String s){
int sum = 0;
for(int i=0; i<[Link](); i++) {
if([Link]([Link](i))) sum += [Link](i) - 48;
}
return sum;
}
public static void main(String []args){
String s = "@g123mai5l1.c2om";
[Link]("Sum of digits of given string: " +
sumOfDigitsInString(s));
}
}
===================================================================================
================
Q6) Sort the given String based on their word length.
Input as : India is a great nation.
Output as : a is India great nation
Ans:
public class MainClass{
public static String sortBasedOnWordLength(String s){
String []sa = [Link]("\\s");
String res = "";
for(int i=0; i<[Link]; i++){
for(int j=i+1; j<[Link]; j++) if(sa[i].length() >
sa[j].length()){
String t = sa[i];
sa[i] = sa[j];
sa[j] = t;
}
res += sa[i] + " ";
}
return res;
}
public static void main(String []args){
String s = "India is a great nation";
[Link]("After sorting based on word length: " +
sortBasedOnWordLength(s));
}
}