Amazon Interview Question
Software Engineer in TestsCountry: United States
Let the input string be “i like this program very much”
Algorithm:
1) Reverse the individual words, we get the below string.
"i ekil siht margorp yrev hcum"
2) Reverse the whole string from start to end and you get the desired output.
"much very program this like i"
check this link for more detail : www(dot)geeksforgeeks(dot)org/archives/7150
string word = "I like this world";
string[] words = word.Split(' ');
for (int i = words.Length - 1; i >= 0; i--)
{
Console.WriteLine(words[i]);
}
Console.WriteLine("**********");
for (int i = 0; i < words.Length; i++)
{
char[] chars = words[i].ToCharArray();
for (int j = chars.Length - 1; j >= 0; j--)
{
Console.Write(chars[j]);
}
Console.WriteLine();
}
Console.ReadLine();
out:
world
this
like
I
********
I
ekil
siht
dlrow
import java.util.Hashtable;
import java.io.Console;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Reverse {
public static void main(String[] args) {
String str1;
Console console = System.console();
str1 = console.readLine("Enter the string: ");
//reverse the whole string
String s=reverseString(str1);
//split the string and reverse all the substrings
String[] strArr = s.split(" ");
// StringBuilder sb = new StringBuilder();
String result = new String();
for(String part:strArr)
{
result += reverseString(part) + " ";
}
System.out.println(result);
}
// 12345 6789
// 9876 54321
// 6789 12345
private static String reverseString(String str)
{
char[] ch = str.toCharArray();
int i=0;
int j = ch.length-1;
char c;
while (i<j)
{
c = ch[j];
ch[j] = ch[i];
ch[i] = c;
i++;
j--;
}
return new String(ch);
}
}
//program to print the string in the reverse order but not the words present in it
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char * arrayin);
void rev_words(char * arrayin);
int main()
{
printf("Enter the string\n");
char input[100];
gets(input);
reverse(input);
rev_words(input);
printf("The output is:\n");
puts(input);
return 0;
}
void reverse(char * arrayin)
{
int i;
int length;
char temp;
length = strlen(arrayin);
for(i=0;i<(length/2);i++)
{
temp = *(arrayin+i);
*(arrayin+i) = *(arrayin+length-1-i);
*(arrayin+length-1-i) = temp;
}
}
void rev_words(char * arrayin)
{
int i,length, count,lengthx;
int start, end,m,j;
char temp;
count = 0;
j = 0;
length = strlen(arrayin);
for(i=0; i<length; i++)
{
if(arrayin[i]==' ')
{
count = count + 1;
}
}
start = 0;
end = 0;
for(i=0;i<length;i++)
{
j = 0;
if(arrayin[i]==' ' || i == (length-1))
{
if(i!= (length-1))
{
end = i - 1;
}
else if(i == (length-1))
{
end = i;
}
lengthx = end - start + 1;
for(m=start; m <= (start - 1 + lengthx/2); m++,j++)
{
temp = arrayin[m];
arrayin[m] = arrayin[end - j];
arrayin[end - j] = temp;
}
start = i + 1;
}
}
}
Using C++ and STL
#include <string>
#include <sstream>
int main(int argc, const char * argv[])
{
string s("Somewhere \n down the road");
//For replace stringstream by fstream for files
istringstream iss(s);
ostringstream oss ("");
string sub;
do
{
iss >> sub; // all delims might be lost - check getline syntax to keep line integrity
streamsize pos = oss.tellp();
oss.str( sub + " " + oss.str() ); // prepend substring, separator
oss.seekp( pos + sub.length() );
cout << "Substring: " << sub << endl;
} while (!iss.eof());
//oss.seekp(0);
cout <<"Reversed String: "<< oss.str();
return 0;
}
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
void ReverseString (char word[], int start, int end) {
char tmp;
while (end > start) {
tmp = word[start];
word[start++] = word[end];
word[end--] = tmp;
}
}
void ReverseWord (char word[]) {
int start = 0, end = 0, length = strlen (word);
ReverseString (word, start, length-1);
while (end < length) {
if (word[end] != ' ') {
start = end;
while ((end < length) && (word[end] != ' ')) {
end++;
}
end--;
ReverseString(word, start, end);
}
end++;
}
}
int main () {
char word[] = "HELLO WORLD. THIS IS FIRST SUCCESSFUL PROGRAM.";
ReverseWord(word);
cout << word;
getch();
return 0;
}
I have some simple string reversal code that uses recursion, sometimes they like to see that kind of stuff.
It is fairly straigh forward
- M.Zimmerman6 December 08, 2012