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 explore 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
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