Linkedin Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
Java version
public class reverseStringRecursive {
private static void reverse(char[] s,int start,int end){
if(start > end){
System.out.println("Reversed String " + String.valueOf(s));
return;
}
if(start == end)
return;
char temp = s[start];
s[start] = s[end];
s[end] = temp;
reverse(s,start+1,end-1);
}
public static void main(String args[]){
String s = "This is a test";
char []temp = s.toCharArray();
reverse(temp,0,temp.length-1);
System.out.println(s);
}
}
class StringReverse {
public static void main(String []args) {
System.out.println(revString("This is a test"));
}
static String revString(String str) {
System.out.println(str);
int length = str.length()-1;
if ( length ==0 )
return String.valueOf(str.charAt(0));
if ( length == 1 )
return String.valueOf(str.charAt(1)) + String.valueOf(str.charAt(0));
return String.valueOf(str.charAt(length)) + revString(str.substring(1, length)) + String.valueOf(str.charAt(0));
}
}
public class RevString
{
public String rev(String s)
{
if(s.length() == 0)
{
return " ";
}
else
{
return s.charAt(s.length()-1)+ rev(s.substring(0, s.length()-1));
}
}
public static void main(String args[])
{
RevString r = new RevString();
String s = new String("This is a test");
System.out.println(r.rev(s));
}
}
public class ReverseString
{
public static void main(String[] args)
{
String test = "This is a test";
System.out.println(test);
System.out.println(reverseString(test));
}
public static String reverseString(String s)
{
if (s == null || "".equals(s))
{
return "";
}
int length = s.length();
if (s.length() == 1)
{
return s;
}
return s.charAt(length - 1) + reverseString(s.substring(1, length - 1)) + s.charAt(0);
}
}
//// Its a Executed Code...
public class StringReverseRecursion
{
public static void main(String args[])
{
String s1 = "prashanth";
reverse(s1,0,s1.length()-1);
}
public static void reverse(String s,int i, int length)
{
if(i == length)
{// donothing
}
else
reverse(s.substring(1),i = i+1, length);
System.out.print(s.charAt(0));
}
}
#include <stdio.h>
#include <string.h>
void f(char *s, char *rev, int i, int j, int size)
{
if(*(s+i) == '\0') {
*(rev+i+size) = '\0';
return;
}
f(s, rev, i+1, j-1, size);
*(rev+j) = *(s+i);
}
int main()
{
char s[] = {"this is test"};
char *rev = malloc(strlen(s));
f(s, rev, 0, strlen(s), strlen(s));
rev++;
printf("revers is %s\n", rev);
return 0;
}
public class RevrString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "This is a string to revrese";
revstr(s,s.length()-1);
}
private static void revstr(String s, int last) {
// TODO Auto-generated method stub
if(last<0)return;
char c = s.charAt(last);
System.out.print(c);
revstr(s,last-1);
}
There are three types of problems possible with String reverse
1) Reverse the whole string:
Input: This is a test
Output: tset a si sihT
2) Reverse words in place
Input: This is a test
Output: sihT si a tset
3) Reverse only the words:
Input: This is a test
Output: test a is This
So for first problem just we need to divide this into smaller problem to reverse this string
Then second one can be done by just reversing the Sub strings in place
Then third one , just a combination of first and two. Reverse this string, then reverse the words in place.
package com.problems;
public class ReverseString
{
public static void main(String[] args)
{
String test = " This is a test ";
System.out.println("Orignal String: " + test);
char[] chars = reverseString(test.toCharArray(), 0, test.toCharArray().length - 1);
System.out.println(String.valueOf("Reverse string: " + String.valueOf(chars)));
chars = reverseStringWordsInPlace(test.toCharArray());
System.out.println(String.valueOf("Reverse words in place: " + String.valueOf(chars)));
chars = reverseStringWordsInPlace(chars);
System.out.println(String.valueOf("Reverse only words: " + String.valueOf(chars)));
}
public static char[] reverseStringWordsInPlace(char[] chars)
{
int start = 0, end = 0;
for (int i = 0; i < chars.length; i++)
{
if (" ".equals(String.valueOf(chars[i])))
{
reverseString(chars, start, i - 1);
start = i + 1;
}
}
reverseString(chars, start, chars.length - 1);
return chars;
}
public static char[] reverseString(char[] chars, int start, int end)
{
if (end < start)
{
return chars;
}
if (chars.length == 0 || chars.length == 1)
{
return chars;
}
swap(chars, start, end);
return reverseString(chars, ++start, --end);
}
public static char[] swap(char[] chars, int i, int j)
{
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
return chars;
}
public static String reverseString(String s)
{
if (s == null || "".equals(s))
{
return "";
}
int length = s.length();
if (s.length() == 1)
{
return s;
}
return s.charAt(length - 1) + reverseString(s.substring(1, length - 1)) + s.charAt(0);
}
}
StringBuilder text = new StringBuilder("This is test");
revString(text);
private static void revString(StringBuilder text) {
revRecu(text, 0, text.length() -1);
System.out.println(text);
}
private static void revRecu(StringBuilder text, int i, int j) {
if (i < j) {
char temp = text.charAt(i);
text.setCharAt(i, text.charAt(j));
text.setCharAt(j, temp);
revRecu(text, i+1, j-1);
}
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(reverse("This is a test"));
}
public static String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
------------------------------
Output: tset a si sihT
public static void main(String[] args) {
System.out.println(reverseStringRecu("this is awesome!", new StringBuilder()));
}
private static String reverseStringRecu(String stringToReverse, StringBuilder sb) {
if(stringToReverse == null || stringToReverse.trim().equals("")) {
return stringToReverse;
}
int len = stringToReverse.length();
if(len ==1 ){
sb.append(stringToReverse);
return sb.toString();
}
int last = len - 1;
sb.append(stringToReverse.charAt(last));
return reverseStringRecu(stringToReverse.substring(0, last), sb);
}
Solution in C:
#include <stdio.h>
#include <string.h>
#define SWAP(a,b) { char t = a; a = b; b = t; }
void rev_str(char* s, char *r)
{
if(s < r - 1)
rev_str(s+1, r-1);
SWAP(*r,*s);
}
int main(void)
{
char str[] = "This is a test";
char *end = str + strlen(str) - 1;
rev_str(str, end);
printf("rev: %s", str);
return 0;
}
public static void reverse(char a[], int low, int high) {
if(low < high) {
char ch = a[low];
a[low] = a[high];
a[high] = ch;
reverse(a, low+1, high-1);
}
}
public static void main(String[] args) {
String str = "This is test";
char a[] = str.toCharArray();
reverse(a, 0, str.length()-1);
System.out.println(new String(a));
}
{{ /*
IIIT-HYD*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define FOR(a,b,c) for(int a=b;a<c;a++)
#define sii(a,b) scanf("%d %d",&a,&b)
#define si(a) scanf("%d",&a)
#define pii(a,b) printf("%d %d\n",a,b)
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,1,sizeof(a))
string word="";
void rever(string a){
if(a.size()==0){
return;
}
rever(a.substr(1));
word=word+a[0];
}
int main(){
string a;
getline(cin,a);
rever(a);
cout << word << endl;
return 0;
}
}}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void reverseString(char *str){
if(*str == '\0')
return ;
reverseString(str+1);
printf("%c", *str);
}
int main()
{
printf("Reverse a string recursively\n");
printf("\n Please enter a string to reverse \n");
char *str= malloc(sizeof(100*sizeof(char)));
scanf("%[^\n]s", str);
reverseString(str);
return 0;
}
public class ReverseStringRecursive {
static String reverseString(String input,int index,String returnString){
if(index<0){
return returnString;
}
else{
returnString =returnString+ input.charAt(index);
input = input.substring(0,index);
index--;
return reverseString(input,index,returnString);
}
}
public static void main(String[] args){
String input = "input string";
String retString ="";
int index = input.length()-1;
System.out.println(reverseString(input,index,retString));
}
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("");
reverseString(sb, "this is a test", 14 );
}
public static void reverseString(StringBuilder reversedString, String originalString, int length){
if(originalString.length() == 0){
System.out.println(reversedString);
return;
}
reversedString.insert(0, originalString.charAt(0));
String originalString2 = originalString.substring(1,length);
reverseString(reversedString, originalString2, length-1);
}
- chaitanya.jun12 March 21, 2013