String Programs in Java With Examples

String is the most widely used class in java programming. That’s why String programs are used in java interviews to access the coding skills.

String Programs in Java

Here I am providing some string programs in java to help you in brushing up your coding skills. Please try to solve these questions yourself before checking the answers to learn in a better way.

I am trying to use all the latest functionalities introduced in java, such as Stream, lambda expressions, functional interfaces etc.

How to get distinct characters and their count in a String?

Write a java program to reverse a String?

There are many ways to reverse a String. Some of the common ones are:

  • StringBuilder/StringBuffer reverse() method
  • Using char/byte array and traverse in reverse direction and populate the result string

However if you are not sure of input String content, always use StringBuilder built-in reverse() method. Because using char and byte array may produce unwanted results. I have provided a complete explanation for this in Reverse a String in Java.

How to check if a String is Palindrome?

A palindrome string is one whose reverse is also same string. So we can reverse the input string and check if both strings are equal for this. Or we can be smart and use the String charAt(int index) method to check for palindrome string.

How to remove all occurrences of a given character from input String?

There is no remove function in the String class, but we can use replaceAll() in this case. Here is the simple program showing how to do it.

How to prove String is immutable programatically?

We know that String is immutable in java, however new developers still get confused with this.

Let’s try to understand the reason for this confusion.

In above code snippet, we can say that s1 value got changed and it’s a String object. So how can we say that String is immutable?

The most important point to understand is how Strings get created in java. When we create a String using string literal, it doesn’t change the value of original String. It creates a new String in the string pool and change the reference of the variable. So original string value is never changed and that’s why Strings are immutable.

Below program proofs our statement, read out the comments for proper understanding the concept.

Write a program to count number of words in a String?

The simple solution to this program seems to be input.split(" ").length but this won’t work if your string is not properly formatted and it contains leading and trailing spaces, duplicate multiple spaces and tabs.

Luckily String split() function takes regular expression as argument and we can use it to count the number of words in a string.

Write a program to check if two Strings are created with same characters?

First of all we will have to create a Set of characters from the input Strings. Then use Set equals() method to check if they contains the same characters or not. Here is a simple program to check if two strings are created with same characters.

Read two String user input and check if first contains second?

This is a simple program and we can use String contains() method to check if specified string is part of this string. However we will have to use Scanner class to read user inputs.

Here is a sample output of above program:

How to swap two Strings without using a third variable?

We can do it using String substring() method. Here is a simple code snippet to showcase this:

What if we have to write a function to do this? Since String is immutable, the change in values of the String references in the method will be gone as soon as the method ends. Also we can’t return multiple objects from a method in java. So we will have to create a Container to hold the input strings and then perform the above logic in the method. Below code shows how this can be done, although it might look complex but the logic is same as above.

Sample Output:

Write a program to find out first non repeated character from input String?

Provide two ways to check if a String contains only digits?

We can use regular expression to check if a String is numeric or not. Another way is to parse it to Long and if it’s a non numeric string then it will throw NumberFormatException.

How to perform Deep Copy for String?

String is immutable, so we don’t need to worry about deep copy or shallow copy. We can simply use assignment operator (=) to copy one string to another. Read more details at Java String copy.

By admin

Leave a Reply

%d bloggers like this: