DURGASOFT
welcome
3
05. Impl prg to sort all the characters in asc/desc order.
----------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
String s = obj.nextLine();
System.out.println(s);
char[] ch = s.toCharArray();
Arrays.sort(ch);
String ss = new String(ch);
System.out.println(ss);
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
welcome
welcome
ceelmow
06. Impl prg to check whether the given strs are anagrams or not.
----------------------------------------------------------------
"race" and "care"
4 and 4
['a','c','e','r'] and ['a','c','e','r']
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s1 = "race";
String s2 = "care";
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
281 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
String s3 = "cary";
char[] ch1 = s1.toCharArray();
char[] ch2 = s2.toCharArray();
char[] ch3 = s3.toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
Arrays.sort(ch3);
System.out.println(Arrays.equals(ch1,ch2));//true
System.out.println(Arrays.equals(ch1,ch3));//false
}
}
07. Impl prg to check whether the given str is paliandrome or not.
------------------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "madam";
String ss = new StringBuffer(s).reverse().toString();
System.out.println(s.equals(ss));//true
}
}
08. Impl prg to check whether the given str is pangram or not.
--------------------------------------------------------------
all english alphabets should be there in that string
s = "abcdefghijklmnopqrstuvwxyz" true
s = "abcdefghijkmnopqrstuvwxyz" false
s = "the quick brown fox jumps over lazy dog" true
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
282 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
{
String s = "the quick brown fox jumps over lazy dog";
boolean flag = true;
for(int i='a';i<='z';i++){
if(s.indexOf(i)<0)
{
flag=false;
break;
}
}
System.out.println(flag);//true
}
}
09. Impl prg to divide the strings seperated by spaces/comma/-.
---------------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens())
System.out.println(st.nextToken());
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
the
quick
brown
fox
jumps
over
lazy
dog
10. Impl prg to reverse the entire sentence.
--------------------------------------------
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
283 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringBuffer sb = new StringBuffer(s);
sb.reverse();
System.out.println(sb);
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
god yzal revo spmuj xof nworb kciuq eht
11. Impl prg to reverse individual words.
-----------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens())
System.out.print(new StringBuffer(st.nextToken()).reverse()+" ");
}
}
C:\3pm>java Test
eht kciuq nworb xof spmuj revo yzal god
C:\3pm>
12. Impl prg to reverse alternative words.
------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
284 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
int i=0;
System.out.println(s);
while(st.hasMoreTokens())
{
if(i%2==0)
System.out.print(st.nextToken()+" ");
else
System.out.print(new StringBuffer(st.nextToken()).reverse()+" ");
i++;
}
}
}
C:\3pm>java Test
the quick brown fox jumps over lazy dog
the kciuq brown xof jumps revo lazy god
13. Impl prg to reverse even/odd length words.
----------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
StringBuffer sb = new StringBuffer();
int i=0;
System.out.println(s);
while(st.hasMoreTokens())
{
String ss = st.nextToken();
if(ss.length()%2==0)
sb.append(new StringBuffer(ss).reverse());
else
sb.append(ss);
sb.append(" ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
285 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
}
System.out.println(sb);
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
the quick brown fox jumps over lazy dog
the quick brown fox jumps revo yzal dog
14. Impl prg to convert every word first char into caps.
--------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
StringBuffer sb = new StringBuffer();
System.out.println(s);
while(st.hasMoreTokens())
{
String ss = st.nextToken();
sb.append(ss.substring(0,1).toUpperCase()+ss.substring(1));
sb.append(" ");
}
System.out.println(sb);
}
}
C:\3pm>java Test
the quick brown fox jumps over lazy dog
The Quick Brown Fox Jumps Over Lazy Dog
15. Impl prg to convert every word first and last char into caps.
-----------------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
286 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
StringBuffer sb = new StringBuffer();
System.out.println(s);
while(st.hasMoreTokens())
{
String ss = st.nextToken();
int n=ss.length();
sb.append(ss.substring(0,1).toUpperCase()+ss.substring(1,n-
1)+ss.substring(n-1,n).toUpperCase());
sb.append(" ");
}
System.out.println(sb);
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
the quick brown fox jumps over lazy dog
ThE QuicK BrowN FoX JumpS OveR LazY DoG
16. Impl prg to convert except first and last chars, remaining into upper case.
-------------------------------------------------------------------------------
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
StringBuffer sb = new StringBuffer();
System.out.println(s);
while(st.hasMoreTokens())
{
String ss = st.nextToken();
int n=ss.length();
sb.append(ss.substring(0,1)+ss.substring(1,n-
1).toUpperCase()+ss.substring(n-1,n));
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
287 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
sb.append(" ");
}
System.out.println(sb);
}
}
output:
-------
C:\3pm>javac Test.java
C:\3pm>java Test
the quick brown fox jumps over lazy dog
tHe qUICk bROWn fOx jUMPs oVEr lAZy dOg
17. American keyboard
----------------------
Given a string, return the true if that can be typed using letters of alphabet on only
one row's of American keyboard like the image below.
In the American keyboard:
=> the first row consists of the characters "qwertyuiop",
=> the second row consists of the characters "asdfghjkl", and
=> the third row consists of the characters "zxcvbnm".
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
String s = obj.nextLine();
String r1 = "qwertyuiop";
String r2 = "asdfghjkl";
String r3 = "zxcvbnm";
int c1=0,c2=0,c3=0;
for(int i=0;i<s.length();i++){
if(r1.contains(s.charAt(i)+""))
c1++;
if(r2.contains(s.charAt(i)+""))
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
288 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
c2++;
if(r3.contains(s.charAt(i)+""))
c3++;
}
System.out.println(c1==s.length()||c2==s.length()||c3==s.length());
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
mom
false
C:\3pm>java Test
dad
true
C:\3pm>java Test
false
false
C:\3pm>java Test
true
true
18. Rotate String
-----------------
Given two strings s and ss, return true if and only if s can become ss after some
number of shifts on s. A shift on s consists of moving the leftmost character of s to the
rightmost position.
For example, if s = "abcde", then it will be "bcdea" after one shift.
s = "abcde"
"abcde"
"bcdea"
"cdeab"
"deabc"
"eabcd"
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
289 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com
DURGASOFT
"bcdea" ---> true
"bdcea" ---> false
"abcdeabcde".contains(ss)
import java.util.*;
import java.util.regex.*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
String s = obj.nextLine();
String ss = obj.nextLine();
System.out.println((s+s).contains(ss));
}
}
C:\3pm>javac Test.java
C:\3pm>java Test
abcde
bcdea
true
C:\3pm>java Test
abcde
bdcea
false
19. Impl prg to return middle char(s).
--------------------------------------
abc ----> b
abcd ---> bc
0123
4 ---> n/2-1 and n/2
3 ---> n/2
import java.util.*;
import java.util.regex.*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
290 88 85 25 26 27, 72 07 21 24 27/28 | www.durgasoftonline.com
Maii: durgasoftonline@gmail.com