Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
static String reverseWords(String string){
char array[] = reverseArray(string.toCharArray(),0,string.length());
System.out.println(new String(array));
int start = 0;
for (int i = 0; i <= array.length; i++) {
if(i == array.length || array[i] == ' ' ){
array = reverseArray(array,start, i-start);
start = i+1;
}
}
return new String(array);
}
static char[] reverseArray(char array[],int start,int length){
for (int i = 0; i < length/2; i++) {
char tmp = array[i+start];
array[i+start] = array[start+length-1-i];
array[start+length-1-i] = tmp;
}
return array;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char* pstr) {
char* start = pstr;
if (pstr == NULL)
return;
while (*pstr && *pstr != ' ')
pstr++;
if (*pstr == ' ') {
*pstr = '\0';
pstr++;
}
while (*pstr && *pstr == ' ')
pstr++;
if (*pstr == '\0'){
printf("%s ",start);
} else {
reverse(pstr);
printf("%s ",start);
}
}
int main()
{
char* str = "This is Java";
int l = strlen(str);
char* ptrf = (char*)calloc(l+1,1);
memcpy(ptrf,str,l);
printf("%s\n",ptrf);
reverse(ptrf);
printf("\b\n");
free(ptrf);
ptrf = NULL;
getchar();
}
public class Reverse_String
{
public static void main(String arg[])
{
String data="This is java";
StringBuffer res=new StringBuffer();
char data_array[]=data.toCharArray();
System.out.println(data.substring(8,11));
int i=data.length()-1;
int d=0;
while(i>=0)
{
d=1;
while(data.charAt(i)!=' ' && i>0)
{
d=d+1;
i=i-1;
}
if(i==0)
res.append(data.substring(i,i+d).trim());
else
res.append(data.substring(i,i+d).trim() + " ");
i=i-1;
}
System.out.println(res.toString());
}
}
<pre lang="" line="1" title="CodeMonkey9696" class="run-this">public class Reverse_String
{
public static void main(String arg[])
{
String data="This is java";
StringBuffer res=new StringBuffer();
char data_array[]=data.toCharArray();
System.out.println(data.substring(8,11));
int i=data.length()-1;
int d=0;
while(i>=0)
{
d=1;
while(data.charAt(i)!=' ' && i>0)
{
d=d+1;
i=i-1;
}
if(i==0)
res.append(data.substring(i,i+d).trim());
else
res.append(data.substring(i,i+d).trim() + " ");
i=i-1;
}
System.out.println(res.toString());
}
}
</pre><pre title="CodeMonkey9696" input="yes">
input:this is java
output:java is this</pre>
How about this.????
public class ReverseString {
static String reverse(String str){
str = new StringBuffer(str).reverse().toString();
StringBuffer res = new StringBuffer();
for(int i=0;i<str.length();){
StringBuffer temp = new StringBuffer();
while(i<str.length() && str.charAt(i)!= ' '){
temp.append(str.charAt(i));
i++;
}
res.append(temp.reverse());
while(i<str.length() && str.charAt(i) == ' ')
i++;
if(i<str.length())
res.append(' ');
}
return res.toString();
}
public static void main(String []args){
System.out.println(reverse("This is Java"));
}
}
static void reverseString(String srt, int idx)
{
int endIdx = idx;
while(endIdx < srt.length() && srt.charAt(endIdx) != ' ' )
endIdx++;
int passOnIdx = endIdx;
while(passOnIdx < srt.length() && srt.charAt(passOnIdx) == ' ' )
passOnIdx++;
if(passOnIdx < srt.length())
reverseString(srt,passOnIdx);
System.out.print(srt.substring(idx, endIdx)+" ");
}
1. Reverse the entire string once
- Sani October 29, 20112. Now reverse each word is the resultant string