Apple Microsoft Interview Question Software Engineer / Developers Software Engineer in Tests
1of 0 votesReverse a string , . don't use any temp variable to store the string .
use the XOR
try this
int a = 'a', b = 'b';
System.out.println(Integer.toBinaryString(a));
System.out.println(Integer.toBinaryString(b));
a = a^b;
System.out.println(Integer.toBinaryString(a));
b = a^b;
System.out.println(Integer.toBinaryString(b));
a = a^b;
System.out.println(Integer.toBinaryString(a));
Nope, that won't work. Consider having the same character. Then, if you xor any two characters, you would get a 0 since xor only returns 1 if one of the other is different. So, 00 returns 0, 11 returns 0. 01 returns 1 and 10 returns 1
void swap (char &a, char &b){a = a ^ b;b = a ^ b;a = a ^ b;}
void reverse(string &s){for(int i=0;i<s.length()/2;i++)swap(s[i],s[s.length()-i-1]);}
int main()
{
string s;cin>>s;
reverse(s);
cout<<s<<endl;
system("pause");
}
#include <stdlib.h>
#include <stdio.h>
void reverse(char* str)
{
int i, j, len;
char temp;
i=j=len=temp=0;
j=strlen(str) - 1;
while(i < j)
{
str[i] = str[i] ^ str[j];
str[j] = str[i] ^ str[j];
str[i] = str[i] ^ str[j];
i++;j--;
}
}
int main()
{
char str[]="hello world";
reverse(str);
printf("%s",str);
getch();
}
//inplace reverse
#include<iostream>
using namespace std;
void main()
{
int length=0;;
char str[7]= "abcdef";
while(str[length] != '\0')
length++;
int i=0;
while(i<length/2)
{
str[i] = str[i] + str[length - i-1];
str[length -i-1] = str[i]- str[length - i-1];;
str[i] = str[i] - str[length -i-1];
i++;
}
cout<<str;
}
void reverse( char *s) {
int i,n=strlen(n);
char *p=s+n-1;
for (i=0;i<n/2; i++) {
*s = *s ^ *p;
*s = *s ^ *p;
*s = *s ^ *p;
s++; p--;
}
}
Generally, with this question the interviewer wants to see how you deal with recursion. For extra marks, you should modify it to the tail-recursive version (Google "tail recursion").
For Java engineers, here's mine - I convert to char array to avoid multiple substring calls ... and to make the tail recursion easier:
public String reverse(String text) {
return reverseHelper(text.length() - 1, text.toCharArray());
}
private String reverseHelper(int index, char[] text) {
return index < 0? "" : text[index] + reverseHelper(index - 1, text);
}
How about:
#include <stdio.h>
#include <string.h>
void reverse(char *stringToReverse);
int main (int argc, const char * argv[]) {
char str[] = "fours";
reverse(str);
printf("%s", str);
return 0;
}
void reverse(char *stringToReverse) {
int start = 0;
int temp = strlen(stringToReverse);
int end = temp - 1;
while (end >= start) {
stringToReverse[temp] = stringToReverse[start];
stringToReverse[start] = stringToReverse[end];
stringToReverse[end] = stringToReverse[temp];
start++;
end--;
}
stringToReverse[temp] = '\0';
}
<pre lang="" line="1" title="CodeMonkey52687" class="run-this">public class ReverseString {
public static void main(String args[])
{
String str = "";
String reversedStr = "";
Scanner input = new Scanner(System.in);
input.useDelimiter(System.getProperty("line.separator"));
System.out.println("Enter a string to reverse: ");
str = input.next();
if(str != null && str.trim().length()!=0)
{
reversedStr = reversedStr.concat(str.substring(str.length()-1));
for(int i=str.length()-2; i>=0; i--)
{
System.out.println(reversedStr);
reversedStr = reversedStr.concat(str.substring(i, i+1));
}
}
System.out.println(reversedStr);
}
}</pre>
Java code Eg: revString("Submit".toCharArray(), 0, 5)
public static void revString(char[] charArr, int start , int end){
while(start < end){
if(charArr[start] != charArr[end]){
charArr[start] = (char) (charArr[start] + charArr[end]);
charArr[end] = (char) (charArr[start] - charArr[end]);
charArr[start] = (char) (charArr[start] - charArr[end]);
}
start++;
end--;
}
}
Use recursive function
*sPtr is the pointer to the input string
void reverse(char *sPtr)
{
if (*sPtr[0]!='\0')
{
reverse(&sPtr[1]);
printf("%c",*sPtr[0]);
}
else
{
return;
}
}

Anyone has a solution to this one? Does it mean we can't use any external memory? Then we have to use XOR to exchange two characters, right?
- Anonymous on February 13, 2009 Edit | Flag Reply