Salesforce Interview Question
Quality Assurance EngineersTeam: Quality Engineering
Country: United States
ublic static void main(String[] args) {
String str;
Scanner sc=new Scanner(System.in);
System.out.println("enter stirng which is to reverse");
str=sc.nextLine();
System.out.println(str);
System.out.println("reverse stirng is:");
int len=str.length();
for (int i = len-1; i >= 0; i--) {
System.out.print(str.charAt(i));
import java.io.*;
class Reverse{
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer(br.readLine());
System.out.println(sb.reverse());
}
}
Here's one more efficient way:
private String doReverseSmartly(String str, int length) {
int n = length - 1;
char []strArray = str.toCharArray();
for (int j = (n - 1) >> 1; j >= 0; --j) {
char temp = strArray[j];
char temp2 = strArray[n - j];
strArray[j] = temp2;
strArray[n - j] = temp;
}
return Arrays.toString(strArray);
}
}
private String doReverseSmartly(String str, int length) {
int n = length - 1;
char []strArray = str.toCharArray();
for (int j = (n - 1) >> 1; j >= 0; --j) {
char temp = strArray[j];
char temp2 = strArray[n - j];
strArray[j] = temp2;
strArray[n - j] = temp;
}
return Arrays.toString(strArray);
}
}
public static String Reverse (String inArgs){
char[] charOrig = inArgs.toCharArray();
char temp;
for (int i= 0; i< inArgs.length()/2; i++) {
temp = charOrig[i];
charOrig [i] = charOrig [inArgs.length ()-i-1];
charOrig [inArgs.length ()-i-1] = temp;
}
return String.copyValueOf(charOrig);
}
Code in c:
char * strrev(char *str)
{
if (str==null)
return null;
int i,l=0;
//measuring length of string
while(str[l]!='\0')
l++;
//swaping first nth an last nth till middle of string
for(i=0;i!=l/2;i++){
//swaping first and last nth char using xor operation
str[i]=str[l-i-1]^str[i];
str[l-i-1]=str[l-i-1]^str[i];
str[i]=str[l-i-1]^str[i];
}
}
A neater implementation can be to use Stack since this is clearly the case of LIFO (Last in First out). I will explain using 'Java'
input : 'Hello World'
output : 'dlroW olleH'
Clearly, this is a first in, last out.
'H' is first in the original string, last in the output string.
'e' is second in the original string, second last in the output string.
Hence, a perfect place to use stacks.
Steps:
1. Read character one by one from left of the string and push it into the stack until you reach end of the string.
2. While stack is not empty, pop character from the stack and add it to the output.
Classes and methods:
Stack.java //Data structure for stack
StringReversal.java //Utility class to reverse a string
Tests.java //Junit Test class
-------------
Stack.java
-------------
public class Stack {
int maxSize;
int top;
char[] stack;
Stack(int size){
maxSize = size;
top=-1;
stack = new char[maxSize];
}
public void push(char c){
if(isFull()){
System.out.println("Can't push. Array is full");
return;
}
stack[++top]=c;
}
public char pop(){
if(isEmpty()){
System.out.println("Can't pop. Array is empty");
return '\n';
}
return stack[top--];
}
public boolean isEmpty(){
return (top==-1);
}
public boolean isFull(){
return(top==maxSize-1);
}
}
------------------------
StringReversal.java
------------------------
public class StringReversal {
public static String reverse(String str) throws NullPointerException{
if(str==null){
throw new NullPointerException("str cannot be null");
}
Stack stack = new Stack(str.length());
for(char c:str.toCharArray()){
stack.push(c);
}
StringBuffer sb = new StringBuffer();
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
}
-------------
Tests.java
-------------
public class StringReversalTests {
@Test
public void testPushPop() {
Stack stack = new Stack(5);
stack.push('a');
Assert.assertEquals('a', stack.pop());
}
@Test
public void testIsEmpty() {
Stack stack = new Stack(5);
//stack should be empty now
Assert.assertTrue(stack.isEmpty());
//Fill some items
stack.push('a');
stack.push('b');
//isEmpty should return false now
Assert.assertFalse(stack.isEmpty());
}
@Test
public void testIsFull(){
Stack stack = new Stack(5);
//isFull should return false now
Assert.assertFalse(stack.isFull());
//Fill some items
stack.push('a');
stack.push('b');
//isFull should return false even now
Assert.assertFalse(stack.isFull());
//fill the entire stack
stack.push('c');
stack.push('d');
stack.push('e');
//Stack is full, assert it is
Assert.assertTrue(stack.isFull());
}
@Test
public void testReverse(){
Assert.assertEquals("dlroW olleH", StringReversal.reverse("Hello World"));
//Test for empty string
Assert.assertEquals("",StringReversal.reverse(""));
//Test for null string
boolean gotNPE=false;
try{
StringReversal.reverse(null);
} catch (NullPointerException npe){
gotNPE = true;
}
Assert.assertTrue(gotNPE);
}
//TODO : Add more test cases for completeness
}
<pre lang="" line="1" title="CodeMonkey93998" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
- merry September 21, 2011class Main
{
private static String reverse(String s) {
char temp;
int l = s.length();
char[] a = s.toCharArray();
for(int i =0;i<a.length/2;i++){
temp=a[i];
a[i]=a[l-i-1];
a[l-i-1]=temp;
}
return new String(a);
}
Output: dlroW olleH
public static void main (String[] args) throws java.lang.Exception
{
String str="Hello World"
System.out.print(reverse(str));
}
}
</pre><pre title="CodeMonkey93998" input="yes">
String str="Hello World"</pre>