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 = "GeeksGeeks",
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 = “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.
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: "Geeks"
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 = "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"
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 = "geeks"
str2 = "Geeks"
Output: False
Explanation: As 'g' is not equal to 'G' so we can't consider them equal.
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!
Contact Us