Java String Exercise
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.
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.
2. Java String Program to Insert a String into Another String
Input: originalString = "BeginnerBeginner",
stringToBeInserted = "For",
index = 4
Output: "w3wiki"
Explanation: Adding the new String to orignal String at the index given.
3. Java String program to check whether a string is a Palindrome
Input: str = “Beginner”
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.
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.
5. Java String Program to Reverse a String
Input: "Beginner"
Output: skeeG
6. Java String Program to Swapping Pair of Characters
Input: str = “w3wiki”
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 "
7. Java String Program to Replace a Character at a Specific Index
Input: str = "Beginner for Beginner" , index = 10 , ch = 'G'
Output: "Beginner for Beginner"
Explanation: String str is "Beginner for Beginner" , as we can see index 10 refers to "Beginner for geeks" this element . So , we will replace it with 'G'.
Result becomes "Beginner for Beginner"
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.
9. Java String Program to Sort a String
Input : "software"
Output : "aeforstw"
10. Java String Program to Compare Two Strings
Input: str1 = "Beginner"
str2 = "Beginner"
Output: False
Explanation: As 'g' is not equal to 'G' so we can't consider them equal.
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
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.
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".
14. Program to Validate an IP address
Input : "125.512.100.1"
Output : Valid
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.
More Java Practice Exercises
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.
Contact Us