Open In App

Java String Exercise

Last Updated : 25 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

String in Java are the objects which can store characters of values in Java, it act the same as an array of characters in Java. Java String is one of the most important topics in Java programming. It is widely used to manipulate and process textual data. In this article, we will learn about Java String with some Java Practice Problems.

Take a look at our Java String Exercise, which will cover the Practice Problems in Java with a series of Java String exercise questions that will help you practice and reinforce your knowledge of String manipulation in Java. These practice problems are both beginner-friendly and experienced-friendly. So, let’s dive into the world of Java String exercises and enhance your programming abilities!

Beginner Questions

Ready to test your Java string skills? This section offers beginner-friendly exercises.  Practice creating, modifying, and working with strings to build a solid foundation for your Java programming journey.

String Practice Problems

1. Java String Program to Print Even-Length Words

Input: s = "This is a java language"

Output: This is java language

Explanation: All the elements with the length even are printed.
"This" length is 4 so printed whereas "a" length is 1 so not Printed.

Click Here to Check the Solution

2. Java String Program to Insert a String into Another String

Input: originalString = "GeeksGeeks", 
stringToBeInserted = "For",
index = 4

Output: "GeeksForGeeks"

Explanation: Adding the new String to orignal String at the index given.

Click Here to Check the Solution

3. Java String program to check whether a string is a Palindrome

Input: str = “geeks”

Output: No 

Explanation: Palindrome is String which can be read same both forth and reverse side or can be said String whose orignal string is same as reverse of String.
"AbbA" , "DaD" , etc these are some examples of Palindrom String.

Click Here to Check the Solution

4. Java String Program to Check Anagram

Input: str1 = "Listen" 
str2 = "Silent"

Output: Yes

Explanation: A string is called Anagram of other string when the it contains the same characters, only the order of characters can be different.
Example Listen -> E:1 , I:1 , L: 1 , N:1 , S:1 , T:1
Silent -> E:1 , I:1 , L: 1 , N:1 , S:1 , T:1

As the occurence of elements are same in both the String hence they are anagram of each other.

Click Here to Check the Solution

5. Java String Program to Reverse a String

Input: "Geeks"

Output: skeeG

Click Here to Check the Solution

6. Java String Program to Swapping Pair of Characters

Input: str = “GeeksForGeeks”

Output: eGkeFsroeGkes

Explanation: Swap Pair of Characters
Pairs to Swap: {G e} , {e k} , { s F } , { o r } , { G e } , { e k } , { s }
After Swap: { e G } , { k e } , { F s } , { r o } , { e G } , { k e } , { s }
Result: " eGkeFsroeGkes "

Click Here to Check the Solution

7. Java String Program to Replace a Character at a Specific Index

Input: str = "Geeks for geeks" , index = 10 , ch = 'G'

Output: "Geeks for Geeks"

Explanation: String str is "Geeks for geeks" , as we can see index 10 refers to "Geeks for geeks" this element . So , we will replace it with 'G'.
Result becomes "Geeks for Geeks"

Click Here to Check the Solution

8. Java String Program to Remove Leading Zeros

Input: 000012356098

Output: 12356098

Explanation: Removing all the elements from the beginning of String which doesn't add any value to the number.

Click Here to Check the Solution

9. Java String Program to Sort a String

Input : "software"

Output : "aeforstw"

Click Here to Check the Solution

10. Java String Program to Compare Two Strings

Input: str1 = "geeks" 
str2 = "Geeks"

Output: False

Explanation: As 'g' is not equal to 'G' so we can't consider them equal.

Click Here to Check the Solution

Advanced DSA Level Problems

This section unleashes advanced DSA-level string problems.  Tackle patterns, searches, and transformations to become a Java string pro.

11. Program to Find the Sum of Two Large Numbers.

Input  : str1 = "7777555511111111", 
str2 = "3332222221111"

Output : 7780887733332222

Click Here to Check the Solution

12. Program to Extract Substring from a String with Equal 0, 1, and 2.

Input: str = “102100211”

Output: 5

Explanation: "102" , "021" , "210" , " 021" , "210021" these are combinations can be formed where the occurrence of 0 , 1 and 2 all are equal.

Click Here to Check the Solution

13. Program to Add Binary Strings

Input : str1 = "1001"
str2 = "11"

Output : "1100"

Explanation : "1001" represents for 9 and "11" represents for 3 then result should be 12 which means result = "1100".

Click Here to Check the Solution

14. Program to Validate an IP address

Input : "125.512.100.1"

Output : Valid

Click Here to Check the Solution

15. Program to Print all Permutations of a String in Java

Input :  abc

Output : abc , acb , bac , bca , cab , cba

Explanation: All the sequence can be formed using these the string will be printed.

Click Here to Check the Solution

More Java Practice Exercises

To Practice Java Online please check our Practice Portal. <- Click Here

Conclusion

Once you have completed the Java String Exercises!  Now you can handle strings like a pro.  Feeling confident? Don’t stop practicing! Play around with these exercises again and try your own ideas to become a Java string master in no time.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads