KLL

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

1.

Write a Java program to get the character at the given index within the
String. Go to the editor

Sample Output:
Original String = Java Exercises!
The character at position 0 is J
The character at position 10 is i

public class Exercise1 {


public static void main(String[] args)
{
String str = "Java Exercises!";
System.out.println("Original String = " + str);
// Get the character at positions 0 and 10.
int index1 = str.charAt(0);
int index2 = str.charAt(10);

// Print out the results.


System.out.println("The character at position 0 is " +
(char)index1);
System.out.println("The character at position 10 is " +
(char)index2);
}
}

2. Write a Java program to get the character (Unicode code point) at the given
index within the String. Go to the editor

Sample Output:
Original String : w3resource.com
Character(unicode point) = 51
Character(unicode point) = 101

public static void main(String[] args) {

String str = "w3resource.com";


System.out.println("Original String : " + str);

// codepoint at index 1
int val1 = str.codePointAt(1);

// codepoint at index 9
int val2 = str.codePointAt(9);

// prints character at index1 in string


System.out.println("Character(unicode point) = " + val1);
// prints character at index9 in string
System.out.println("Character(unicode point) = " + val2);
}
}

3. Write a Java program to get the character (Unicode code point) before the
specified index within the String. Go to the editor

Sample Output:
Original String : w3resource.com
Character(unicode point) = 119
Character(unicode point) = 99
public static void main(String[] args) {

String str = "w3resource.com";


System.out.println("Original String : " + str);

// codepoint before index 1


int val1 = str.codePointBefore(1);

// codepoint before index 9


int val2 = str.codePointBefore(9);

// prints character before index1 in string


System.out.println("Character(unicode point) = " + val1);
// prints character before index9 in string
System.out.println("Character(unicode point) = " + val2);
}
}

4. Write a java program to count a number of Unicode code points in the


specified text range of a String. Go to the editor

Sample Output:
Original String : w3rsource.com
Codepoint count = 9
public static void main(String[] args) {

String str = "w3rsource.com";


System.out.println("Original String : " + str);

// codepoint from index 1 to index 10


int ctr = str.codePointCount(1, 10);

// prints character from index 1 to index 10


System.out.println("Codepoint count = " + ctr);
}
}

5. Write a java program to compare two strings lexicographically. Go to the


editor

Sample Output:
String 1: This is Exercise 1
String 2: This is Exercise 2
"This is Exercise 1" is less than "This is Exercise 2"
public class Exercise5 {

public static void main(String[] args)

String str1 = "This is Exercise 1";

String str2 = "This is Exercise 2";

System.out.println("String 1: " + str1);

System.out.println("String 2: " + str2);

// Compare the two strings.

int result = str1.compareTo(str2);

// Display the results of the comparison.

if (result < 0)

System.out.println("\"" + str1 + "\"" +

" is less than " +

"\"" + str2 + "\"");

else if (result == 0)

System.out.println("\"" + str1 + "\"" +

" is equal to " +


"\"" + str2 + "\"");

else // if (result > 0)

System.out.println("\"" + str1 + "\"" +

" is greater than " +

"\"" + str2 + "\"");

6. Write a java program to compare two strings lexicographically, ignoring


case differences. Go to the editor

Sample Output:
String 1: This is exercise 1
String 2: This is Exercise 1
"This is exercise 1" is equal to "This is Exercise 1"
Click me to see the solution
public class Exercise6 {

public static void main(String[] args)

String str1 = "This is exercise 1";

String str2 = "This is Exercise 1";

System.out.println("String 1: " + str1);

System.out.println("String 2: " + str2);

// Compare the two strings.

int result = str1.compareToIgnoreCase(str2);


// Display the results of the comparison.

if (result < 0)

System.out.println("\"" + str1 + "\"" +

" is less than " +

"\"" + str2 + "\"");

else if (result == 0)

System.out.println("\"" + str1 + "\"" +

" is equal to " +

"\"" + str2 + "\"");

else // if (result > 0)

System.out.println("\"" + str1 + "\"" +

" is greater than " +

"\"" + str2 + "\"");

7. Write a Java program to concatenate a given string to the end of another


string. Go to the editor

Sample Output:
String 1: PHP Exercises and
String 2: Python Exercises
The concatenated string: PHP Exercises and Python Exercises
Click me to see the solution
public class Exercise7 {
public static void main(String[] args)
{
String str1 = "PHP Exercises and ";
String str2 = "Python Exercises";

System.out.println("String 1: " + str1);


System.out.println("String 2: " + str2);

// Concatenate the two strings together.


String str3 = str1.concat(str2);

// Display the new String.


System.out.println("The concatenated string: " + str3);
}
}

8. Write a Java program to test if a given string contains the specified


sequence of char values. Go to the editor

Sample Output:
Original String: PHP Exercises and Python Exercises
Specified sequence of char values: and
true
Click me to see the solution
public class Exercise8 {

public static void main(String[] args)


{
String str1 = "PHP Exercises and Python Exercises";
String str2 = "and";
System.out.println("Original String: " + str1);
System.out.println("Specified sequence of char values: " +
str2);
System.out.println(str1.contains(str2));
}
}

9. Write a Java program to compare a given string to the specified character


sequence. Go to the editor

Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
Click me to see the solution
public class Exercise9 {
public static void main(String[] args) {
String str1 = "example.com", str2 = "Example.com";
CharSequence cs = "example.com";
System.out.println("Comparing "+str1+" and "+cs+": " +
str1.contentEquals(cs));
System.out.println("Comparing "+str2+" and "+cs+": " +
str2.contentEquals(cs));
}
}

10. Write a Java program to compare a given string to the specified string
buffer. Go to the editor

Sample Output:
Comparing example.com and example.com: true
Comparing Example.com and example.com: false
Click me to see the solution
public class Exercise10 {

public static void main(String[] args) {

String str1 = "example.com", str2 = "Example.com";


StringBuffer strbuf = new StringBuffer(str1);

System.out.println("Comparing "+str1+" and "+strbuf+": " +


str1.contentEquals(strbuf));

System.out.println("Comparing "+str2+" and "+strbuf+": " +


str2.contentEquals(strbuf));

}
}

11. Write a Java program to create a new String object with the contents of a
character array. Go to the editor

Sample Output:
The book contains 234 pages.
Click me to see the solution

public class Exercise11 {


public static void main(String[] args)

// Character array with data.

char[] arr_num = new char[] { '1', '2', '3', '4' };

// Create a String containig the contents of arr_num

// starting at index 1 for length 2.

String str = String.copyValueOf(arr_num, 1, 3);

// Display the results of the new String.

System.out.println("\nThe book contains " + str +" pages.\n");

12. Write a Java program to check whether a given string ends with the
contents of another string. Go to the editor

Sample Output:
"Python Exercises" ends with "se"? false
"Python Exercise" ends with "se"? true
Click me to see the solution

public class Exercise12 {

public static void main(String[] args)

String str1 = "Python Exercises";


String str2 = "Python Exercise";

// The String to check the above two Strings to see

// if they end with this value (se).

String end_str = "se";

// Check first two Strings end with end_str

boolean ends1 = str1.endsWith(end_str);

boolean ends2 = str2.endsWith(end_str);

// Display the results of the endsWith calls.

System.out.println("\"" + str1 + "\" ends with " +

"\"" + end_str + "\"? " + ends1);

System.out.println("\"" + str2 + "\" ends with " +

"\"" + end_str + "\"? " + ends2);

13. Write a Java program to check whether two String objects contain the
same data. Go to the editor

Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "Mike Royko"? false
Click me to see the solution
public class Exercise13 {

public static void main(String[] args)


{
String columnist1 = "Stephen Edwin King";
String columnist2 = "Walter Winchell";
String columnist3 = "Mike Royko";

// Are any of the above Strings equal to one another?


boolean equals1 = columnist1.equals(columnist2);
boolean equals2 = columnist1.equals(columnist3);

// Display the results of the equality checks.


System.out.println("\"" + columnist1 + "\" equals \"" +
columnist2 + "\"? " + equals1);
System.out.println("\"" + columnist1 + "\" equals \"" +
columnist3 + "\"? " + equals2);
}
}

14. Write a Java program to compare a given string to another string, ignoring
case considerations. Go to the editor

Sample Output:
"Stephen Edwin King" equals "Walter Winchell"? false
"Stephen Edwin King" equals "stephen edwin king"? true
Click me to see the solution

public class Exercise14 {

public static void main(String[] args)

String columnist1 = "Stephen Edwin King";

String columnist2 = "Walter Winchell";

String columnist3 = "stephen edwin king";

// Test any of the above Strings equal to one another

boolean equals1 = columnist1.equalsIgnoreCase(columnist2);

boolean equals2 = columnist1.equalsIgnoreCase(columnist3);


// Display the results of the equality checks.

System.out.println("\"" + columnist1 + "\" equals \"" +

columnist2 + "\"? " + equals1);

System.out.println("\"" + columnist1 + "\" equals \"" +

columnist3 + "\"? " + equals2);

15. Write a java program to print current date and time in the specified
format. Go to the editor

Sample Output:
Current Date and Time :
June 19, 2017
3:13 pm
N.B. : The current date and time will change according to your system date and time.
Click me to see the solution

import java.util.Calendar;

public class Exercise15 {

public static void main(String[] args) {

Calendar c = Calendar.getInstance();

System.out.println("Current Date and Time :");

System.out.format("%tB %te, %tY%n", c, c, c);

System.out.format("%tl:%tM %tp%n", c, c, c);

}
}

16. Write a Java program to get the contents of a given string as a byte
array. Go to the editor

Sample Output:
The new String equals This is a sample String.
Click me to see the solution

import java.util.Calendar;

public class Exercise16 {

public static void main(String[] args)

String str = "This is a sample String.";

// Copy the contents of the String to a byte array.

byte[] byte_arr = str.getBytes();

// Create a new String using the contents of the byte array.

String new_str = new String(byte_arr);

// Display the contents of the byte array.

System.out.println("\nThe new String equals " +

new_str + "\n");

}
}

17. Write a Java program to get the contents of a given string as a character
array. Go to the editor

Sample Output:
The char array equals "[C@2a139a55"
Click me to see the solution

public class Exercise17 {

public static void main(String[] args)

String str = "This is a sample string.";

// Copy the contents of the String to a byte array.

// Only copy characters 4 through 10 from str.

// Fill the source array starting at position 2.

char[] arr = new char[] { ' ', ' ', ' ', ' ',

' ', ' ', ' ', ' ' };

str.getChars(4, 10, arr, 2);

// Display the contents of the byte array.

System.out.println("The char array equals \"" +

arr + "\"");

}
18. Write a Java program to create a unique identifier of a given string. Go to
the editor

Sample Output:
The hash for Python Exercises. is 863132599
Click me to see the solution

public class Exercise18 {

public static void main(String[] args)

String str = "Python Exercises.";

// Get the hash code for the above string.

int hash_code = str.hashCode();

// Display the hash code.

System.out.println("The hash for " + str +

" is " + hash_code);

19. Write a Java program to get the index of all the characters of the
alphabet. Go to the editor

Sample Output:
a b c d e f g h i j
=========================
36 10 7 40 2 16 42 1 6 20

k l m n o p q r s t
===========================
8 35 22 14 12 23 4 11 24 31

u v w x y z
================
5 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."

Click me to see the solution

public class Exercise19 {

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";

// Get the index of all the characters of the alphabet

// starting from the beginning of the String.

int a = str.indexOf("a", 0);

int b = str.indexOf("b", 0);

int c = str.indexOf("c", 0);

int d = str.indexOf("d", 0);

int e = str.indexOf("e", 0);

int f = str.indexOf("f", 0);

int g = str.indexOf("g", 0);

int h = str.indexOf("h", 0);

int i = str.indexOf("i", 0);

int j = str.indexOf("j", 0);


int k = str.indexOf("k", 0);

int l = str.indexOf("l", 0);

int m = str.indexOf("m", 0);

int n = str.indexOf("n", 0);

int o = str.indexOf("o", 0);

int p = str.indexOf("p", 0);

int q = str.indexOf("q", 0);

int r = str.indexOf("r", 0);

int s = str.indexOf("s", 0);

int t = str.indexOf("t", 0);

int u = str.indexOf("u", 0);

int v = str.indexOf("v", 0);

int w = str.indexOf("w", 0);

int x = str.indexOf("x", 0);

int y = str.indexOf("y", 0);

int z = str.indexOf("z", 0);

// Display the results of all the indexOf method calls.

System.out.println(" a b c d e f g h i j");

System.out.println("=========================");

System.out.println(a + " " + b + " " + c + " " + d + " " +

e+""+f+""+g+""+h+""+

i + " " + j + "\n");


System.out.println("k l m n o p q r s t");

System.out.println("===========================");

System.out.println(k + " " + l + " " + m + " " + n + " " +

o+""+p+""+q+""+r+""+

s + " " + t + "\n");

System.out.println("u v w x y z");

System.out.println("================");

System.out.println(u + " " + v + " " + w + " " + x + " " +

y + " " + z);

20. Write a Java program to get the canonical representation of the string
object. Go to the editor

Sample Output:
str1 == str2? false
str1 == str3? true
Click me to see the solution

public class Exercise20 {

public static void main(String[] args)

// Create three strings in three different ways.

String str1 = "Java Exercises";

String str2 = new StringBuffer("Java").append(" Exercises").toString();


String str3 = str2.intern();

// Determine which strings are equivalent using the ==

// operator (as compared to calling equals(), which is

// a more expensive operation.

System.out.println("str1 == str2? " + (str1 == str2));

System.out.println("str1 == str3? " + (str1 == str3));

21. Write a Java program to get the last index of a string within a string. Go to
the editor

Sample Output:
a b c d e f g h i j
===========================
36 10 7 40 33 16 42 32 6 20

k l m n o p q r s t
===========================
8 35 22 14 41 23 4 29 24 31

u v w x y z
=================
21 27 13 18 38 37
Sample string of all alphabet: "The quick brown fox jumps over the lazy dog."

Click me to see the solution

public class Exercise21 {

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";
// Get the index of all the characters of the alphabet

// starting from the beginning of the String.

int a = str.lastIndexOf("a", str.length() - 1);

int b = str.lastIndexOf("b", str.length() - 1);

int c = str.lastIndexOf("c", str.length() - 1);

int d = str.lastIndexOf("d", str.length() - 1);

int e = str.lastIndexOf("e", str.length() - 1);

int f = str.lastIndexOf("f", str.length() - 1);

int g = str.lastIndexOf("g", str.length() - 1);

int h = str.lastIndexOf("h", str.length() - 1);

int i = str.lastIndexOf("i", str.length() - 1);

int j = str.lastIndexOf("j", str.length() - 1);

int k = str.lastIndexOf("k", str.length() - 1);

int l = str.lastIndexOf("l", str.length() - 1);

int m = str.lastIndexOf("m", str.length() - 1);

int n = str.lastIndexOf("n", str.length() - 1);

int o = str.lastIndexOf("o", str.length() - 1);

int p = str.lastIndexOf("p", str.length() - 1);

int q = str.lastIndexOf("q", str.length() - 1);

int r = str.lastIndexOf("r", str.length() - 1);

int s = str.lastIndexOf("s", str.length() - 1);

int t = str.lastIndexOf("t", str.length() - 1);

int u = str.lastIndexOf("u", str.length() - 1);


int v = str.lastIndexOf("v", str.length() - 1);

int w = str.lastIndexOf("w", str.length() - 1);

int x = str.lastIndexOf("x", str.length() - 1);

int y = str.lastIndexOf("y", str.length() - 1);

int z = str.lastIndexOf("z", str.length() - 1);

// Display the results of all the lastIndexOf method calls.

System.out.println(" a b c d e f g h i j");

System.out.println("===========================");

System.out.println(a + " " + b + " " + c + " " + d + " " +

e+""+f+""+g+""+h+""+

i + " " + j + "\n");

System.out.println("k l m n o p q r s t");

System.out.println("===========================");

System.out.println(k + " " + l + " " + m + " " + n + " " +

o+""+p+""+q+""+r+""+

s + " " + t + "\n");

System.out.println(" u v w x y z");

System.out.println("=================");

System.out.println(u + " " + v + " " + w + " " + x + " " +

y + " " + z);


}

22. Write a java program to get the length of a given string. Go to the editor

Sample Output:
The string length of 'example.com' is: 11
Click me to see the solution

public class Exercise22 {

public static void main(String[] args)

String str = "example.com";

// Get the length of str.

int len = str.length();

System.out.println("The string length of '"+str+"' is: "+len);

23. Write a Java program to find whether a region in the current string
matches a region in another string. Go to the editor

Sample Output:
str1[0 - 7] == str2[28 - 35]? true
str1[9 - 15] == str2[9 - 15]? false

Click me to see the solution

public class Exercise23 {

public static void main(String[] args)

{
//String str1 = "Red Green Orange Yellow";

//String str2 = "Yellow Orange Green Red";

String str1 = "Shanghai Houston Colorado Alexandria";

String str2 = "Alexandria Colorado Houston Shanghai";

// Determine whether characters 0 through 7 in str1

// match characters 28 through 35 in str2.

boolean match1 = str1.regionMatches(0, str2, 28, 8);

// Determine whether characters 9 through 15 in str1

// match characters 9 through 15 in str2.

boolean match2 = str1.regionMatches(9, str2, 9, 8);

// Display the results of the regionMatches method calls.

System.out.println("str1[0 - 7] == str2[28 - 35]? " + match1);

System.out.println("str1[9 - 15] == str2[9 - 15]? " + match2);

24. Write a Java program to replace all the 'd' characters with 'f'
characters. Go to the editor

Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown fox jumps over the lazy fog.
Click me to see the solution
public class Exercise24 {

public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";

// Replace all the 'd' characters with 'f' characters.

String new_str = str.replace('d', 'f');

// Display the strings for comparison.

System.out.println("Original string: " + str);

System.out.println("New String: " + new_str);

25. Write a Java program to replace each substring of a given string that
matches the given regular expression with the given replacement. Go to the
editor

Sample string : "The quick brown fox jumps over the lazy dog."

In the above string replace all the fox with cat.

Sample Output:
Original string: The quick brown fox jumps over the lazy dog.
New String: The quick brown cat jumps over the lazy dog.
Click me to see the solution

public class Exercise25 {


public static void main(String[] args)

String str = "The quick brown fox jumps over the lazy dog.";

// Replace all the 'dog' with 'cat'.

String new_str = str.replaceAll("fox", "cat");

// Display the strings for comparison.

System.out.println("Original string: " + str);

System.out.println("New String: " + new_str);

26. Write a Java program to check whether a given string starts with the
contents of another string. Go to the editor

Sample Output:
Red is favorite color. starts with Red? true
Orange is also my favorite color. starts with Red? false
Click me to see the solution

public class Exercise26 {

public static void main(String[] args)

String str1 = "Red is favorite color.";

String str2 = "Orange is also my favorite color.";


// The String to check the above two Strings to see

// if they start with this value (Red).

String startStr = "Red";

// Do either of the first two Strings start with startStr?

boolean starts1 = str1.startsWith(startStr);

boolean starts2 = str2.startsWith(startStr);

// Display the results of the startsWith calls.

System.out.println( str1 + " starts with " +

startStr + "? " + starts1);

System.out.println(str2 + " starts with " +

startStr + "? " + starts2);

27. Write a Java program to get a substring of a given string between two
specified positions. Go to the editor

Sample Output:
old = The quick brown fox jumps over the lazy dog.
new = brown fox jumps
Click me to see the solution

public class Exercise27 {

public static void main(String[] args)

{
String str = "The quick brown fox jumps over the lazy dog.";

// Get a substring of the above string starting from

// index 10 and ending at index 26.

String new_str = str.substring(10, 26);

// Display the two strings for comparison.

System.out.println("old = " + str);

System.out.println("new = " + new_str);

28. Write a Java program to create a character array containing the contents
of a string. Go to the editor

Sample Output:
Java Exercises.
Click me to see the solution

public class Exercise28 {

public static void main(String[] args)

String str = "Java Exercises.";

// Convert the above string to a char array.

char[] arr = str.toCharArray();


// Display the contents of the char array.

System.out.println(arr);

29. Write a Java program to convert all the characters in a string to


lowercase. Go to the editor

Sample Output:
Original String: The Quick BroWn FoX!
String in lowercase: the quick brown fox!
Click me to see the solution

public class Exercise29 {

public static void main(String[] args)

String str = "The Quick BroWn FoX!";

// Convert the above string to all lowercase.

String lowerStr = str.toLowerCase();

// Display the two strings for comparison.

System.out.println("Original String: " + str);

System.out.println("String in lowercase: " + lowerStr);

}
30. Write a Java program to convert all the characters in a string to
uppercase. Go to the editor

Sample Output:
Original String: The Quick BroWn FoX!
String in uppercase: THE QUICK BROWN FOX!
Click me to see the solution

public class Exercise30 {

public static void main(String[] args)

String str = "The Quick BroWn FoX!";

// Convert the above string to all uppercase.

String upper_str = str.toUpperCase();

// Display the two strings for comparison.

System.out.println("Original String: " + str);

System.out.println("String in uppercase: " + upper_str);

31. Write a Java program to trim any leading or trailing whitespace from a
given string. Go to the editor

Sample Output:
Original String: Java Exercises
New String: Java Exercises
Click me to see the solution
public class Exercise31 {

public static void main(String[] args)

String str = " Java Exercises ";

// Trim the whitespace from the front and back of the

// String.

String new_str = str.trim();

// Display the strings for comparison.

System.out.println("Original String: " + str);

System.out.println("New String: " + new_str);

}s

32. Write a Java program to find longest Palindromic Substring within a


string. Go to the editor

Sample Output:
The given string is: thequickbrownfoxxofnworbquickthe
The longest palindrome substring in the giv
en string is; brownfoxxofnworb
The length of the palindromic substring is: 16
Click me to see the solution

33. Write a Java program to find all interleavings of given strings. Go to the
editor

Sample Output:
The given strings are: WX YZ
The interleavings strings are:
YWZX
WYZX
YWXZ
WXYZ
YZWX
WYXZ
Click me to see the solution

34. Write a Java program to find the second most frequent character in a
given string. Go to the editor

Sample Output:
The given string is: successes
The second most frequent char in the string is: c
Click me to see the solution

35. Write a Java program to print all permutations of a given string with
repetition. Go to the editor

Sample Output:
The given string is: PQR
The permuted strings are:
PPP
PPQ
PPR
...
RRP
RRQ
RRR
Click me to see the solution

36. Write a Java program to check whether two strings are interliving of a
given string. Assuming that the unique characters in both strings. Go to the
editor

Sample Output:
The given string is: PMQNO
The interleaving strings are MNO and PQ
The given string is interleaving: true

The given string is: PNQMO


The interleaving strings are MNO and PQ
The given string is interleaving: false
Click me to see the solution

37. Write a Java program to find Length of the longest substring without
repeating characters. Go to the editor

Sample Output:
Input String : pickoutthelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8
Click me to see the solution

38. Write a Java program to print after removing duplicates from a given
string. Go to the editor

Sample Output:
The given string is: w3resource
After removing duplicates characters the new string is:
w3resouc
Click me to see the solution

39. Write a Java program to find first non repeating character in a string. Go to
the editor

Sample Output:
The given string is: gibblegabbler
The first non repeated character in String is: i
Click me to see the solution

40. Write a Java program to divide a string in n equal parts. Go to the editor

Sample Output:
The given string is: abcdefghijklmnopqrstuvwxy
The string divided into 5 parts and they are:

abcde
fghij
klmno
pqrst
uvwxy
Click me to see the solution

41. Write a Java program to remove duplicate characters from a given string
presents in another given string. Go to the editor
Sample Output:
The given string is: the quick brown fox
The given mask string is: queen

The new string is:


th ick brow fox
Click me to see the solution

42. Write a Java program to print list items containing all characters of a given
word. Go to the editor

Sample Output:
The given strings are: rabbit bribe dog
The given word is: bib

The strings containing all the letters of the given word are:
rabbit
bribe
Click me to see the solution

43. Write a Java program to find the maximum occurring character in a


string. Go to the editor

Sample Output:
The given string is: test string
Max occurring character in the given string is: t
Click me to see the solution

44. Write a Java program to reverse a string using recursion. Go to the editor

Sample Output:
The given string is: The quick brown fox jumps
The string in reverse order is:
spmuj xof nworb kciuq ehT
Click me to see the solution

45. Write a Java program to reverse words in a given string. Go to the editor

Sample Output:
The given string is: Reverse words in a given string
The new string after reversed the words: string given a in
words Reverse
Click me to see the solution

46. Write a Java program to reverse every word in a string using methods. Go
to the editor

Sample Output:
The given string is: This is a test string
The string reversed word by word is:
sihT si a tset gnirts
Click me to see the solution

47. Write a Java program to rearrange a string so that all same characters
become d distance away. Go to the editor

Sample Output:
The given string is: accessories
The string after arrange newly is:
secrsecisao
Click me to see the solution

48. Write a Java program to remove "b" and "ac" from a given string. Go to the
editor

Sample Output:
The given string is: abrambabasc
After removing the new string is: aramaasc
Click me to see the solution

49. Write a Java program to find first non-repeating character from a stream of
characters. Go to the editor

Sample Output:
String: godisgood
Reading: g
The first non-repeating character so far is: g
Reading: o
The first non-repeating character so far is: g
Reading: d
The first non-repeating character so far is: g
Reading: i
The first non-repeating character so far is: g
Reading: s
The first non-repeating character so far is: g
Reading: g
The first non-repeating character so far is: o
Reading: o
The first non-repeating character so far is: d
Reading: o
The first non-repeating character so far is: d
Reading: d
The first non-repeating character so far is: i
Click me to see the solution

50. Write a Java program to find lexicographic rank of a given string. Go to the
editor

Sample Output:
The Given String is: BDCA
The Lexicographic rank of the given string is: 12
N.B.: Total possible permutations of BDCA are(lexicographic order) :
ABCD ABDC ACBD ACDB ADBC ADCB BACD BADC BCAD BCDA BDAC
BDCA
1 2 3 4 5 6 7 8 9 10 11
12
The BDCA appear in 12 position of permutation (lexicographic order).

Click me to see the solution

51. Write a Java program to count and print all the duplicates in the input
string. Go to the editor

Sample Output:
The given string is: w3resource
The duplicate characters and counts are:
e appears 2 times
r appears 2 times
Click me to see the solution

52. Write a Java program to check if two given strings are rotations of each
other. Go to the editor

Sample Output:
The given strings are: ABACD and CDABA

The concatination of 1st string twice is: ABACDABACD


The 2nd string CDABA exists in the new string.
Strings are rotations of each other
Click me to see the solution

53. Write a Java program to match two strings where one string contains
wildcard characters. Go to the editor

Sample Output:
The given string is: abcdhgh
The given pattern string is: abc*d?*
The given pattern is matching.
Click me to see the solution

54. Write a Java program to find the smallest window in a string containing all
characters of another string. Go to the editor

Sample Output:
The given string is: welcome to w3resource
Characters to find in the main sring are: tower
The smallest window which contains the finding characters is :
to w3re
Click me to see the solution

55. Write a Java program to remove all adjacent duplicates recursively from a
given string. Go to the editor

Sample Output:
The given string is: aabaarbarccrabmq
The new string after removing all adjacent duplicates is:
brmq
Click me to see the solution

56. Write a Java program to append two given strings such that, if the
concatenation creates a double characters then omit one of the
characters. Go to the editor

Sample Output:
The given strings are: food and door
The string after concatination are: foodoor
Click me to see the solution
57. Write a Java program to return a new string where the last two characters
of a given string, if present, are swapped. Go to the editor

Sample Output:
The given strings is: string
The string after swap last two characters are: strign
Click me to see the solution

58. Write a Java program to read a string and return true if it ends in "ng". Go
to the editor

Sample Output:
The given strings is: string
The string containing ng at last: true

The given strings is: strign


The string containing ng at last: false
Click me to see the solution

59. Write a Java program to read a string,if the string begins with
&quot;red&quot; or &quot;black&quot; return that color string, otherwise return
the empty string. Go to the editor

Sample Output:
The given strings is: blacksea
The string begins with the color: black
Click me to see the solution

60. Write a Java program to read two strings append them together and return
the result. If the strings are different lengths, omit chars from the beginning of
longer string and make them equal length. Go to the editor

Sample Output:
The given strings is: Welcome and home
The new string is: comehome
Click me to see the solution

61. Write a Java program to read a string and an int n, return a string made of
the first and last n characters from the string. Go to the editor

Sample Output:
The given strings is: Welcome
The given numbers is: 3
The new string is: Welome
Click me to see the solution

62. Write a Java program to read a string and return true if "good" appears
starting at index 0 or 1 in the given string. Go to the editor

Sample Output:
The given strings is: goodboy
The 'good' appear in the string is: true
Click me to see the solution

63. Write a Java program to return true from a given string if the first two
characters in the string also appear at the end. Go to the editor

Sample Output:
The given strings is: educated
The first two characters appear in the last is: true
Click me to see the solution

64. Write a Java program to read a string and if a substring of length two
appears at both its beginning and end, return a string without the substring at
the beginning otherwise, return the original string unchanged. Go to the editor

Sample Output:
The given strings is: educated
The new string is: ucated
Click me to see the solution

65. Write a Java program to read a string and if the first or last characters are
't', return the string without those 't' otherwise return the string unchanged. Go
to the editor

Sample Output:
The given strings is: testcricket
The new string is: estcricke
Click me to see the solution
66. Write a Java program to read a string and return the string without the first
two characters.Except keep the first char if it is 'g' and keep the second char if
it is 'h'. Go to the editor

Sample Output:
The given strings is: goat
The new string is: gat

he given strings is: photo


The new string is: hoto

The given strings is: ghost


The new string is: ghost
Click me to see the solution

67. Write a Java program to read a string and if one or both of the first tow
characters is 'x', return without those 'x',otherwise return the string
unchanged. Go to the editor

Sample Output:
The given strings is: oocyte
The new string is: cyte

The given strings is: boat


The new string is: bat

The given strings is: own


The new string is: wn
Click me to see the solution

68. Write a Java program to read a string and returns after remove the # and
its immediate left and right characters. Go to the editor

Sample Output:
The given strings is: test#string
The new string is: testring

The given strings is: test##string


The new string is: testring

The given strings is: test#the#string


The new string is: teshtring
Click me to see the solution
69. Write a Java program to return the substring that is between the first and
last appearance of the substring 'toast' in the given string,or return the empty
string if substirng 'toast' does not exists. Go to the editor

Sample Output:
The given strings is: sweettoastbuttertoast
The new string is: butter
Click me to see the solution

70. Write a Java program to check whether a string is pq-balanced or not.A


String is pq-balanced if for all the p's in the string atleast one 'q' must exists
right of the p's.But 'q' before the 'p' makes the pq-balanced false. Go to the
editor

Sample Output:
The given strings is: gfpmpnppqab
The string is pq-balanced? true

The given strings is: gfpmpnpqpab


The string is pq-balanced? false
Click me to see the solution

71. Write a Java program to return true when either of the two given strings
appear at the end of the other string ignoring case sensitivity. Go to the editor

Sample Output:
The given strings are: xyz and pqrxyz
Is one string appears at the end of other? true

The given strings are: pqrxyz and xyz


Is one string appears at the end of other? true
Click me to see the solution

72. Write a Java program to return true if a given string contain the string
'pop', but the middle 'o' also may other character. Go to the editor

Sample Output:
The given string is: dikchapop
Is p?p appear in the given string? true

The given string is: dikp$pdik


Is p?p appear in the given string? true
Click me to see the solution

73. Write a Java program to return true if the given string contains an
appearance of 'abc' but not directly a period(.) and followed by. Go to the
editor

Sample Output:
The given strings is: testabc.test
Is 'abc' appear before period? true

The given string is: test.abctest


Is 'abc' appear before period? false
Click me to see the solution

74. Write a Java program to return whether a prefix string made of the first N
specific characters of the string appear somewhere else in the string. Go to
the editor

Sample Output:
The given strings is: MrsJemsmrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? false

The given string is: MrsJemsMrsam


The prefix string length is: 3
Is 'Mrs' appear else where in the string? true
Click me to see the solution

75. Write a Java program to check whether a string 'abc' in the middle of a
given string. Here middle means the number of character to the left and right
of the substring 'abc' must differ by at most one. Go to the editor

Sample Output:
The given string is: xxxabcxxxxx
Is abc appear in middle? false

The given string is: xxabcxxx


Is abc appear in middle? true
Click me to see the solution

76. Write a Java program to count how many times the substring 'life' present
at anywhere in a given string. Counting can also happen for the substring
'li?e', any character instead of 'f'. Go to the editor
Sample Output:
The given string is: liveonwildlife
The substring life or li?e appear number of times: 2
Click me to see the solution

77. Write a Java program to add a string with specific number of times
seperated by a substring. Go to the editor

Sample Output:
The given strings are: try and best
Number to times to be repeat: 3
The new string is: trybesttrybesttry
Click me to see the solution

78. Write a Java program to repeat a specific number of characters for specific
number of times from the last of a string. Go to the editor

Sample Output:
The given string is: string
The new string after repetition: inginging
Click me to see the solution

79. Write a Java program to return the given string after removing the 2nd
character from the substring of length three, starting with 'z' and ending with
'g'. Go to the editor

Sample Output:
The given string is: zzgkitandkatcaketoket
The new string is: zgkitandkatcaketoket
Click me to see the solution

80. Write a Java program to check whether the character immediately before
and after of # is same in a given string. Go to the editor

Sample Output:
The given string is: moon#night
The before and after character are same: true

The given string is: bat##ball


The before and after character are same: false

The given string is: #moon#night


The before and after character are same: true
Click me to see the solution

81. Write a Java program to check whether the string 'red' and 'blue' appear in
same number of times in a given string. Go to the editor

Sample Output:
The given string is: redcapmanwithbluecar
The appearance of red and blue are same: true
Click me to see the solution

82. Write a Java program to repeat every character twice in the original
string. Go to the editor

Sample Output:
The given string is: welcome
The new string is: wweellccoommee
Click me to see the solution

83. Write a Java program to make a new string from two given string in such a
way that, each character of two string will come respectively. Go to the editor

Sample Output:
The given strings are: welcome and w3resource
The new string is: wwe3lrceosmoeurce
Click me to see the solution

84. Write a Java program to make a new string made of p number of


characters from the first of a given string and followed by p-1 number
characters till the p is greater than zero. Go to the editor

Sample Output:
The given string is: welcome
Number of repetition characters and repetition: 4
The new string is: welcwelwew
Click me to see the solution

85. Write a Java program to make a new string with each character of just
before and after of t-string whichever it appears in m-string. Assume that m-
string and non-empty t-string has given. Go to the editor
Sample Output:
The given string are: weablcoabmeab and ab
The new string is: elome
Click me to see the solution

86. Write a Java program to return the number of triples in the given string. A
triple is a character appearing three times in a row in a string. Go to the editor

Sample Output:
The given string is: welllcommmmeee
The number of triples in the string is: 4
Click me to see the solution

87. Write a Java program to check whether a z is happy or not. A 'z' is happy
when there is another 'z' immediately to its left or right.Return true if all the z's
in the given string are happy. Go to the editor

Sample Output:
The given string is: azzlea
Is z happy in the string: true

The given string is: azmzlea


Is z happy in the string: falses
Click me to see the solution

88. Write a Java program to return a string where every appearance of the
lowercase word 'is' has been replaced with 'is not'. Go to the editor

Sample Output:
The given string is: it is a string
The new string is: it is not a string
Click me to see the solution

89. Write a Java program to return the sum of the numbers (may form more
than one digits), appearing in the string. Go to the editor

Sample Output:
The given string is: it 15 is25 a 20string
The sum of numbers in the string is: 60
Click me to see the solution
90. Write a Java program to return true if the number of appearances of 'the'
and 'is' anywhere in the string is equal. Go to the editor

Sample Output:
The given string is: Thisisthethesis
Are the appearance of 'the' and 'is' equal? false
Click me to see the solution

91. Write a Java program to count the number of words ending in 'm' or 'n' (not
case sensitive). Go to the editor

Sample Output:
The given string is: mam is in the room
The number of words ends eith m or n is: 3
Click me to see the solution

92. Write a Java program to return a substring after removing the all instances
of remove string as given from the given main string. Go to the editor

Sample Output:
The main string is: This is the test string
The removable string is: st
The new string is: This is the te ring
Click me to see the solution

93. Write a Java program to find the longest substring appears at both ends of
a given string. Go to the editor

Sample Output:
The given string is: playersplay
The longest substring in the string is: play
Click me to see the solution

94. Write a Java program to find the longest mirror image string at the both
ends of a given string. Go to the editor

Sample Output:
The given string is: rotavator
The longest mirror image string in the string is: rotavator
Click me to see the solution
95. Write a Java program to return the sum of the digits present in the given
string. If there is no digits the sum return is 0. Go to the editor

Sample Output:
The given string is: ab5c2d4ef12s
The sum of the digits in the string is: 14
Click me to see the solution

96. Write a Java program to return the string after removing all 'z' (except the
very first and last) from a given string. Go to the editor

Sample Output:
The given string is: zebrazone
The new string is: zebraone
Click me to see the solution

97. Write a Java program to return a string with the characters of the index
position 0,1,2, 5,6,7, ... from a given string. Go to the editor

Sample Output:
The given string is: w3resource.com
The new string is: w3rour.co
Click me to see the solution

98. Write a Java program to check whether the first instance of 'z' is
immediately followed by another 'z' in a given string. Go to the editor

Sample Output:
The given string is: fizzez
Is 'z' appear twice respectively? true
Click me to see the solution

99. Write a Java program to return a new string using every characters of
even positions from a given string. Go to the editor

Sample Output:
The given string is: w3resource.com
The new string is: wrsuc.o
Click me to see the solution
100. Write a Java program to check if a given string contains another string.
Return true or false. Go to the editor

Sample Output:
Original string:
Java is the foundation for virtually every type of networked
application and is the global standard for developing and
delivering embedded and mobile applications, games, Web-based
content, and enterprise software. With more than 9 million
developers worldwide, Java enables you to efficiently develop,
deploy and use exciting applications and services.

Is 'million' present in the said text?


true

Is 'millions' present in the said text?


false
Click me to see the solution

101. Write a Java program to test if a given string contains only digits. Go to
the editor

Sample Output:
First string:
131231231231231231231231231212312312
true

Second string:
13123123123Z1231231231231231212312312
false
Click me to see the solution

102. Write a Java program to convert a given String to int, long, float and
double. Go to the editor

Sample Output:
Convert String to int/Integer:
"1323" as int is 1323 and as Integer is 1323

Convert String to long/Long:


"13625478965325" as long is 13625478965325 and as Long is
13625478965325

Convert String to float/Float:


"25.135F" as float is 25.135 and as Float is 25.135
Convert String to double/Double:
"21.25478254D" as double is 21.25478254 and as Double is
21.25478254
false
Click me to see the solution

103. Write a Java program to remove a specified character from a given


string. Go to the editor

Sample Output:
Original string:
abcdefabcdeabcdaaa

Second string:
bcdefbcdebcd
Click me to see the solution

104. Write a Java program to sort in ascending and descending order by


length of the given array of strings. Go to the editor

Sample Output:
Original unsorted colors: [Green, White, Black, Pink, Orange,
Blue, Champagne, Indigo, Ivory]

Sorted color (descending order): [Champagne, Orange, Indigo,


Green, White, Black, Ivory, Pink, Blue]

Sorted color (ascending order): [Pink, Blue, Green, White,


Black, Ivory, Orange, Indigo, Champagne]
Click me to see the solution

105. Write a Java program to count the occurrences of a given string in


another given string. Go to the editor

Sample Output:
aa' has occured 3 times in 'abcd abc aabc baa abcaa'
Click me to see the solution

106. Write a Java program to concatenate a given string with itself of a given
number of times. Go to the editor

Sample Output:
Original string: PHP

After repeating 7 times: PHPPHPPHPPHPPHPPHPPHP


Click me to see the solution

You might also like