0% found this document useful (0 votes)
6 views10 pages

Java With DSA

The document contains Java programs that demonstrate various string manipulation techniques, including sorting characters, checking for anagrams, palindromes, pangrams, reversing strings, and modifying word cases. Each program is accompanied by sample input and output, showcasing its functionality. The document serves as a practical guide for learning string operations in Java.

Uploaded by

Rehan Hussain
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)
6 views10 pages

Java With DSA

The document contains Java programs that demonstrate various string manipulation techniques, including sorting characters, checking for anagrams, palindromes, pangrams, reversing strings, and modifying word cases. Each program is accompanied by sample input and output, showcasing its functionality. The document serves as a practical guide for learning string operations in Java.

Uploaded by

Rehan Hussain
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

DURGASOFT

welcome
3

05. Impl prg to sort all the characters in asc/desc order.


----------------------------------------------------------
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
String s = [Link]();
[Link](s);
char[] ch = [Link]();
[Link](ch);
String ss = new String(ch);
[Link](ss);
}
}

C:\3pm>javac [Link]

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 [Link].*;
import [Link].*;
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 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
String s3 = "cary";

char[] ch1 = [Link]();


char[] ch2 = [Link]();
char[] ch3 = [Link]();

[Link](ch1);
[Link](ch2);
[Link](ch3);

[Link]([Link](ch1,ch2));//true
[Link]([Link](ch1,ch3));//false
}
}

07. Impl prg to check whether the given str is paliandrome or not.
------------------------------------------------------------------
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
String s = "madam";
String ss = new StringBuffer(s).reverse().toString();

[Link]([Link](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 [Link].*;
import [Link].*;
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 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
String s = "the quick brown fox jumps over lazy dog";
boolean flag = true;
for(int i='a';i<='z';i++){
if([Link](i)<0)
{
flag=false;
break;
}
}
[Link](flag);//true
}
}

09. Impl prg to divide the strings seperated by spaces/comma/-.


---------------------------------------------------------------
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
while([Link]())
[Link]([Link]());
}
}

C:\3pm>javac [Link]

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 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringBuffer sb = new StringBuffer(s);
[Link]();
[Link](sb);
}
}

C:\3pm>javac [Link]

C:\3pm>java Test
god yzal revo spmuj xof nworb kciuq eht

11. Impl prg to reverse individual words.


-----------------------------------------
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
String s = "the quick brown fox jumps over lazy dog";
StringTokenizer st = new StringTokenizer(s);
while([Link]())
[Link](new StringBuffer([Link]()).reverse()+" ");
}
}

C:\3pm>java Test
eht kciuq nworb xof spmuj revo yzal god
C:\3pm>

12. Impl prg to reverse alternative words.


------------------------------------------
import [Link].*;
import [Link].*;
class Test
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
284  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
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;
[Link](s);
while([Link]())
{
if(i%2==0)
[Link]([Link]()+" ");
else
[Link](new StringBuffer([Link]()).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 [Link].*;
import [Link].*;
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;
[Link](s);
while([Link]())
{
String ss = [Link]();
if([Link]()%2==0)
[Link](new StringBuffer(ss).reverse());
else
[Link](ss);
[Link](" ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
285  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
}
[Link](sb);
}
}

C:\3pm>javac [Link]

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 [Link].*;
import [Link].*;
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();
[Link](s);
while([Link]())
{
String ss = [Link]();
[Link]([Link](0,1).toUpperCase()+[Link](1));
[Link](" ");
}
[Link](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 [Link].*;
import [Link].*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
286  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
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();
[Link](s);
while([Link]())
{
String ss = [Link]();
int n=[Link]();
[Link]([Link](0,1).toUpperCase()+[Link](1,n-
1)+[Link](n-1,n).toUpperCase());
[Link](" ");
}
[Link](sb);
}
}

C:\3pm>javac [Link]

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 [Link].*;
import [Link].*;
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();
[Link](s);
while([Link]())
{
String ss = [Link]();
int n=[Link]();
[Link]([Link](0,1)+[Link](1,n-
1).toUpperCase()+[Link](n-1,n));
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
287  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
[Link](" ");
}
[Link](sb);
}
}

output:
-------
C:\3pm>javac [Link]

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 [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
String s = [Link]();

String r1 = "qwertyuiop";
String r2 = "asdfghjkl";
String r3 = "zxcvbnm";

int c1=0,c2=0,c3=0;

for(int i=0;i<[Link]();i++){
if([Link]([Link](i)+""))
c1++;
if([Link]([Link](i)+""))
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
288  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
c2++;
if([Link]([Link](i)+""))
c3++;
}

[Link](c1==[Link]()||c2==[Link]()||c3==[Link]());
}
}

C:\3pm>javac [Link]

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 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
"bcdea" ---> true
"bdcea" ---> false

"abcdeabcde".contains(ss)

import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);
String s = [Link]();
String ss = [Link]();
[Link]((s+s).contains(ss));
}
}

C:\3pm>javac [Link]

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 [Link].*;
import [Link].*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
290  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]

You might also like