WAP To Reverse String Without Using Inbuilt Functions

Reverse String Without Using Inbuilt Functions: Welcome to our blog post on the fascinating challenge of reversing a string without relying on any inbuilt functions! In the world of programming, reversing a string is a classic problem that tests your algorithmic thinking and string manipulation skills.

While modern programming languages offer built-in functions to effortlessly achieve this task, we’re here to dive deeper into the underlying principles and unravel the magic behind the scenes.

Reverse String Without Using Inbuilt Functions

This Java program aims to reverse a string without using any built-in functions.

package com.softwaretestingo.interviewprograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InterviewPrograms96 
{
	//Write a program to reverse a string without using inbuilt functions
	private static String reverseString(String str) 
	{
		if (str == null) 
			return null;

		StringBuilder output = new StringBuilder();

		for (int i = str.length() - 1; i >= 0; i--) 
		{
			output.append(str.charAt(i));
		}

		return output.toString();
	}
	public static void main(String[] args) throws IOException 
	{
		String str;

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string");
		str=br.readLine();

		System.out.println("After Reverse the String: "+str);
		System.out.println("After Reverse the String: "+reverseString(str));
		br.close();
	}
}

Output

Enter the string
Software
After Reverse the String: Software
After Reverse the String: erawtfoS

Sure! Let’s break down the Java program step by step:

  • Import Statements: The code begins with import statements, which are used to bring in required classes from Java’s standard library. In this case, we import BufferedReader and IOException to handle user input.
  • Class and Method Declaration: The program defines a class named InterviewPrograms96. Inside the class, there is a private static method reverseString, responsible for reversing the input string. The main method is the entry point of the program.
  • reverseString Method: This method takes a single parameter str, which represents the input string to be reversed. The method returns a reversed version of the input string. It is important to note that if the input string is null, the method returns null.
  • Reversing the String: Inside the reverseString method, a StringBuilder named output is declared. A StringBuilder is an efficient way to modify strings in Java. The method then enters a loop that iterates through the characters of the input string in reverse order. It starts from the last character (i.e., str.length() – 1) and goes up to the first character (i.e., i >= 0). In each iteration, the character at the current index i is appended to the output StringBuilder.
  • User Input: In the main method, the program prompts the user to enter a string using System.out.println(). The user’s input is obtained using a BufferedReader, which reads input from the console. The input string is then stored in the str variable.
  • Output: After reading the input string, the program displays the original input string and then calls the reverseString method to get the reversed version. The reversed string is then displayed using System.out.println().

Summary: The Java program takes a string input from the user, reverses the string without using any built-in functions, and then prints both the original and reversed strings to the console. It uses a loop to iterate through the characters of the input string in reverse order and appends them to a StringBuilder object to form the reversed string. This program is helpful in understanding how to reverse a string using a loop and a StringBuilder in Java.

Alternative Way 1:

package com.softwaretestingo.interviewprograms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InterviewPrograms96_1 
{
	//Write a program to reverse a string without using inbuilt functions
	private static String reverseString(String str) 
	{
		int size;
		if (str == null) 
			return null;

		char arr[]=str.toCharArray();
		size=arr.length;
		for (int i = 0; i <size/ 2; i++) 
		{
			char c = arr[i];
			arr[i] = arr[size - i - 1];
			arr[size - i - 1] = c;
		}

		return new String(arr);
	}
	public static void main(String[] args) throws IOException 
	{
		String str;

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the string");
		str=br.readLine();

		System.out.println("After Reverse the String: "+str);
		System.out.println("After Reverse the String: "+reverseString(str));
		br.close();
	}
}

Output

Enter the string
SoftwareTestingo
After Reverse the String: SoftwareTestingo
After Reverse the String: ognitseTerawtfoS

This Java program is another implementation of reversing a string without using any built-in functions. Let’s walk through the code step by step:

  • Import Statements: Similar to the previous program, this one starts with import statements to include necessary classes from the Java standard library.
  • Class and Method Declaration: The program defines a class named InterviewPrograms96_1. Inside the class, there is a private static method reverseString, responsible for reversing the input string. The main method serves as the entry point of the program.
  • reverseString Method: This method takes a single parameter str, which represents the input string to be reversed. The method returns a reversed version of the input string. If the input string is null, it returns null.
  • Reversing the String: Inside the reverseString method, the input string is converted to a character array using toCharArray(). This allows for direct manipulation of the characters in the string. A variable size is initialized with the length of the character array.The method then uses a loop that iterates through the first half of the character array (up to size/2). In each iteration, it swaps the character at index i with the character at index size – i – 1. This effectively reverses the order of characters in the character array.After completing the loop, the reversed character array is converted back to a string using the new String(arr) constructor, and this reversed string is returned.
  • User Input and Output: The main method prompts the user to enter a string and reads the input using a BufferedReader. The input string is stored in the str variable.The program then displays the original input string and calls the reverseString method to obtain the reversed version. The reversed string is displayed using System.out.println().

In essence, this program achieves the same goal as the previous one but uses a different approach to reverse the string. Instead of iterating from the end of the string to the beginning, it converts the string to a character array and swaps characters from both ends towards the center. This alternative implementation showcases different techniques for string manipulation and presents a great opportunity for Java beginners to grasp the concept of arrays and how to work with them to reverse a string.

Happy coding!

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment