Top 12 String Programs For Your Next Java Interview

Top 12 String Programs For Your Next Java Interview

Top 12 String Programs For Your Next Java Interview

Java String is an important part of the Java Interviews. Below I have given the top 12 Java String programs (coding examples) that you will be asked in your Java Interview. Also, I have provided the required explanation to understand the program in a better way.

Also, I have written a blog about the Top 20 Java String Interview Question And Answers. This post explains the top 20 theoretical questions and answers about the string. Please consider reading this blog before trying the following coding examples.

1. How to convert an array to string in Java?

Sometimes in a program, you have to convert an existing array to string.

There is a total of 4 ways we can convert an array to string.

1. Arrays.toString() method

Arrays.toString() method returns the string of the input array. The returned string is a string representation of the input array.

The returned string contains the elements of the input array separated by the comma and followed by space. Also, the element is surrounded by ‘[]’.

2. StringBuilder.append() method

In this way, we are appending each element of the input array to an instance of the StringBuilder class using the append() method.

By using this method, we can get the string from the elements of the input array connected without spaces.

3. String.Join() method

Java 8 and above, you can use the join() method of the String class converts an array to string.

4. Collectors.joining() method

Java 8 provides the Stream API. You can use the joining() method of the Collectors class to convert an array to string.

The following program shows you how to convert an array to string.

The output of the above program will be,

2. How to check if two strings are the same ignoring their cases?

You can check if two strings are the same or not using the equals() method of the String. There is a special version of the equals() method which ignores the cases i.e. equalsIgnoreCase()

Below I have given the program to check if the string is the same or not using the equalsIgnoreCase().

The output of the above program will be,

In the first case, we are comparing the strings using the equals() method.
It is printing false because even if both the string contains the same sentence, there is a difference between the case.

In firstString every word starts with a capital letter while in secondString, every letter is in a small case.

In the second case, we are comparing the strings using the equalsIgnoreCase() method.

It will compare the content of the firstString and secondString ignoring the case hence it is printing true.

3. How to print duplicate characters from the string?

In this program, you have to found and print the duplicate characters from the given input string.

Sometimes you will be asked to print the number of times that duplicate characters came in the string. So we are printing their count too.

Below I have given a program that prints the duplicate characters from the string with its count.

 

The output of the above program will be,

4. How to check if two strings are anagrams of each other?

We can say if two strings are an anagram of each other if they contain the same characters but at different orders.

Ex. army & mary

This question requires a good understanding of the Java String class and Java arrays.

The following program shows how can we check if the given strings are an anagram of each other.

The output of the above program will be,

In the above program, the isAnagram() method took two string firstString and secondString as an input parameter.

We are converting these string into the character array.

Also, we are applying the toLowerCase() method to the strings to make sure every character of the char array should be in lower case.

Once we got both the array, we are sorting them using the Arrays.sort() method.

Arrays.sort() method sort the specified array into ascending numerical order.

Finally, we will check if the arrays are equals using Arrays.equals() method.

If they are equal, that means they are an anagram of each other.
If they are not equal, then they are not an anagram of each other.

5. How to reverse a string in Java without using the reverse method?

This is the common question asked in the java interviews. We know, we can easily reverse the java string using the reverse() method of the StringBuilder class.

You will be asked to reverse the string without using any pre-defined library reverse method.

Below, I have given a program to reverse the string without using the reverse method.

The output of the above program will be,

In the first method, we are initializing an empty string variable, reversedString.

You can use the charAt() method of the String class to get the character at any particular index of the string.

We are getting the characters of the inputString one by one in reverse order and appending it to the reversedString.

Finally, we are returning the reversedString.

In the second method, we are converting the inputString to the characters array using the toCharArray() method of the String class.

Once we got the array, we are appending the characters from the characters array stringCharArray to reversedString one by one in reverse order.

And finally, we are returning the reversedString.

6. How to count the occurrence of the given character in a string?

Sometimes you will be asked to count the number of occurrences of any particular character in the given input string.

Below, I have given the program for the same.

The output of the above program will be,

In the above program, we are checking the number of occurrences for the character ch in the string inputString.

If the character ch found in the inputString, we are increasing the count by one.

Finally, we are printing the character count using the print statement.

Also, you can use the continue statements to count the occurrences of a character in a string. The continue statement is one of the control flow statements in Java.

Below I have given a program that uses the continue statement to solve the above question.

The output of the above program will be,

7. How to convert string to integer?

In the java program, many times, we have to convert a string to an int.
We can convert string to an integer using Integer.parseInt() method as well as Integer.valueOf() method.

Below I have given a java program to convert the String str to int result.

The output of the above program will be,

In the above program, we have to make sure that String str should not contain any characters other than the decimal digit.

If str contains any character other than digits, it will throw the NumberFormatException.

8. How to check if a string is a palindrome?

If the string is similar from the starting as well as the ending, then we can say that the string is a palindrome.

For example. madam, radar, etc.

I have given a program below which checks if the given string is palindrome or not.

The output of the above program will be,

In the above program, we are getting the reversed string of the inputString.

If the reversedString and the inputString is the same, then the string is a palindrome.

9. Write a program to remove given characters from the string?

The below program removes any specified character from the given string.

The output of the above program will be,

In the above program, we are getting character at any particular index using the charAt() method.

If this character equals the specified character, we will remove this character from the string.

10. How to count the number of words in a given string sentence?

This is a nice question.

You can count the number of words from a given string using the library method like the split() method of the String class, and also you can use the StringTokenizer class to count the number of words.

If the interviewer asks you to create a new method to count the number of words in the sentence, we will see that way too.

The method to count the number of words using the split() method is given below.

In the above method, we are using the "\\s+" for splitting because of the + operator adds in if the string contains more than one space in between the string.

The method to count the number of words using the StringTokenizer class is given below.

In the above program, we are creating a StringTokenizer using the inputString and simply returning the number of tokens using countTokens() method.

When we create tokens using the StringTokenizer, it delimits the inputString using the space character.

The custom method to count the number of words is given below,

Below, I have given the complete program which counts the number of words from the given input string using all the three input methods.

The output of the above program will be,

11. How to reverse the words from the given string sentence?

In the above program we have seen, how can we count the number of words from the input string.

We can use the same logic to get the words. Once we have a word, we can reverse it.

And when we connect these reversed words properly, we will have a string with reversed words.

I have given a full program to reverse the words from the string.

The output of the above program will be,

12. How to swap two strings without using the third variable?

We can swap two string without using a third variable. We are going to use a substring method to achieve the same.

Below I have given a program that swaps the two strings without using the third string.

The output of the above program will be,

In the above program, I have marked the steps.

In the first step, we are just adding both the strings and storing it in a firstString.

Now firstString will be “GauravKukade”.

In the second step, the substring() method with the first parameter ‘0’ and last parameter (firstString.length() - secondString.length()) will give us the first string. We are assigning it to secondString.

In the last step, we will use the substring() method with only one parameter, secondString.length(). (remember now the value of the secondString is “Gaurav”.) It will give us the second string, and we can assign it to the firstString.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

12.10.2020

Java Lambda Expressions

A lambda expression is a piece of code that is giving an alternative way to the anonymous class to pass the function as a parameter to other ...

11.09.2020

Spring Cloud Config Refresh Strategies

In this article, we will focus on how to refresh the configuration data fetched from the Spring Cloud Config ...

26.08.2020

Centralize The Configuration of Services With Spring Cloud Config

In this article, we will look over the Spring Cloud Config project which is one of the main projects under Spring Cloud and that’s mainly designed ...

1 comment

Nikita Bragin March 16, 2020 at 6:07 pm
0

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy