Amazon Interview Question
Quality Assurance EngineersTeam: Amazon Wireless
Country: India
Interview Type: Written Test
What do the interviewer exactly mean by do write program without using any in-built functions?
Is it inclusive of all the library functions like substring, charat,.. and so on or that would be only to avoid using the functions like 'sort' or 'reverse' which would help us in achieving the given problem in a few lines?
package javaProgramming;
public class Test1 {
public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}
System.out.println("Output for "+input+" is : "+sb);
}
}
public static void reverseWords(String str){
StringBuilder string = new StringBuilder(str);
Matcher matcher = Pattern.compile("\\w+").matcher(string);
while(matcher.find()){
reverseStr(string, matcher.start(), matcher.end()-1);
}
System.out.println(string);
}
private static void reverseStr(StringBuilder str, int i, int j){
while(i<j){
char tmp = str.charAt(i);
str.setCharAt(i, str.charAt(j));
str.setCharAt(j, tmp);
i++;
j--;
}
}
string = "This is an example"
x = []
y = ""
for i in range(1, len(string)+1):
j = -1*i
y = y + string[j]
#print (y)
k = ''
for i in range(0, len(string)):
if string[i] != ' ':
k = k +string[i]
if (string[i] == ' ') or (i == (len(string)-1)):
x.append(k)
k = ''
#print (x)
z = []
q = ''
for j in range(0, len(x)):
t = str(x[j])
for p in range(1,len(t)+1):
p = -1*p
q = q + t[p]
z.append(q)
q = ''
#print (z)
rev_str = ''
for g in range(0,len(z)):
rev_str = rev_str + ' ' + str(z[g])
print (rev_str.lstrip())
void reverseEachWord(string& str, int start, int end)
{
while(start<end)
{
swap(str[start++], str[end--]);
}
}
void reverseWord(string& str)
{
int len = str.length();
int start = 0, end = 0;
for(int i=0;i<len;i++)
{
start = i;
end = i;
while(i< len && str[i] != ' ')
{
end++;
i++;
}
reverseEachWord(str, start, end-1);
if(str[i] == ' ')
continue;
}
}
#include<stdio.h>
#include<string.h>
void reverseEachWord(char str[], int start, int end)
{char t;
while(start<end)
{
t=str[start];
str[start]=str[end];
str[end]=t;
start++;
end--;
}
}
char* reverseWord(char str[])
{
int len = strlen(str),i;
int start = 0, end = 0;
for( i=0;i<len;i++)
{
start = i;
end = i;
while(i< len && str[i] != ' ')
{
end++;
i++;
}
reverseEachWord(str, start, end-1);
if(str[i] == ' ')
continue;
}
return str;
}
int main()
{
char a[100];
gets(a);
printf("%s ",reverseWord(a));
It is critical that you divide by 2 or you will get the same word, and you don't have to worry about the median or mid character
function reverseChars(char[] strArray) {
if ( strArray.length < 2 ) return;
int mid = strArray.length/2;
for ( int i = 0; i < mid; ++i ) {
char tmp = strArray[i];
strArray[i] = strArray[strArray.length-i-1];
strArray[strArray.length-i-1] = tmp;
}
}
#include<stdio.h>
#include<string.h>
void reverseEachWord(char str[], int start, int end)
{char t;
while(start<end)
{
t=str[start];
str[start]=str[end];
str[end]=t;
start++;
end--;
}
}
char* reverseWord(char str[])
{
int len = strlen(str),i;
int start = 0, end = 0;
for( i=0;i<len;i++)
{
start = i;
end = i;
while(i< len && str[i] != ' ')
{
end++;
i++;
}
reverseEachWord(str, start, end-1);
if(str[i] == ' ')
continue;
}
return str;
}
int main()
{
char a[100];
gets(a);
printf("%s ",reverseWord(a));
}
String reverse(String seg)
{
int length=seg.length();
char[] cTemp=seg.toCharArray();
for(int front=0, back=length-1;front<=back;front++,back--)
{
char ctemp1=cTemp[front];
cTemp[front]=cTemp[back];
cTemp[back]=ctemp1;
}
String s=new String(cTemp);
return s;
}
String reverseWords(String Words)
{
String[] words1=Words.split(" ");
String ans="";
for(String Temp:words1){
ans=ans+" "+reverse(Temp);
}
return ans;
}
In Java there are two ways:
1:
String array[] = str.split(" ");
for (int i = 0; i < array.length; i++) {
StringBuffer buffer = new StringBuffer();
buffer = buffer.append(array[i]);
buffer = buffer.reverse();
array[i] = buffer.toString();
System.out.print(array[i] + " ");
}
2 :
String array[] = str.split(" ");
for (int i = 0; i < array.length; i++) {
StringBuffer buffer = new StringBuffer();
for (int j = array[i].length() - 1; j >= 0; j--)
{
buffer =buffer.append(array[i].charAt(j));
}
array[i] = buffer.toString();
System.out.print(array[i]+" ");
String array[] = str.split(" ");
//There are two ways
//first
for (int i = 0; i < array.length; i++) { StringBuffer buffer = new
StringBuffer();
for (int j = array[i].length() - 1; j >= 0; j--) { buffer =
buffer.append(array[i].charAt(j));
} array[i] = buffer.toString(); System.out.print(array[i]+" ");
//Second
for (int k= 0; k < array.length; k++) {
StringBuffer buffer1= new StringBuffer();
buffer1 = buffer1.append(array[i]);
buffer1 = buffer1.reverse();
array[i] = buffer1.toString();
System.out.print(array[i] + " ");
}
void reverseWordsInString(string& str)
{
int wordIndex = 0;
for (int i = 0; i <= str.length(); i++) {
if (str[i] == NULL || str[i] == ' ') {
for (int j = i - 1; j >= wordIndex; j--) {
char temp = str[j];
str[j] = str[wordIndex];
str[wordIndex] = temp;
wordIndex++;
}
wordIndex = i + 1;
}
}
}
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char str[100], temp;
int i, j = 0;
printf("\n Enter the string: ");
gets(str);
i = 0;
j = strlen(str) - 1;
while(i<j){
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\n The reversed string is: %s", str);
printf("\n \t The length of the string is: %d", strlen(str));
getch();
}
public static char[] Reverse(char[] str)
{
if (str.Length == 0)
return new char[0];
else if (str.Length == 1)
return new char[1] { str[0] };
char[] res = new char[str.Length];
int si = 0;
int ei = 0;
for (int i = si; i < str.Length; i++, ei++)
{
if (str[i] != ' ')
{
continue;
}
else
{
res[i] = ' ';
for (int j = si; j < ei; j++)
{
res[j] = str[ei - 1 - (j - si)];
}
si = ei + 1;
}
}
if (ei >= str.Length)
ei = str.Length - 1;
if (si <= ei)
{
for (int j = si; j <= ei; j++)
{
res[j] = str[ei - (j - si)];
}
}
return res;
}
The above code is in C# and do not use any inbuilt string functions. It just manipulates the array items
#include<iostream>
#include<string.h>
using namespace std;
int main() //amit don hai
{
int k=0,i,l1,j;
char ch[445];
gets(ch);
l1=strlen(ch);
for(i=k;i<l1;i++)
{
if(ch[i]==' ' || ch[i]=='\0')
{
for(j=i-1;j>=k;j--)
cout<<ch[j];
cout<<" ";
k=i+1;
}
else if(i==l1-1)
{
for(j=l1-1;j>=k;j--)
cout<<ch[j];
}
}
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main() //amit don hai
{
int k=0,i,l1,j;
char ch[445];
gets(ch);
l1=strlen(ch);
for(i=k;i<l1;i++)
{
if(ch[i]==' ' || ch[i]=='\0')
{
for(j=i-1;j>=k;j--)
cout<<ch[j];
cout<<" ";
k=i+1;
}
else if(i==l1-1)
{
for(j=l1-1;j>=k;j--)
cout<<ch[j];
}
}
return 0;
}
#include <iostream>
using namespace std;
void reverseString(string& s){
int length = s.size() -1;
for(int i=0; i<=length/2;i++){
swap(s[i],s[length-i]);
}
}
int main(){
string str ="This is an example";
cout << "The original string is: " << str << endl;
reverseString(str);
cout << "The reversed string is: " << str << endl;
return 0;
}
#include <iostream>
using namespace std;
void reverseString(string& s){
int length = s.size() -1;
for(int i=0; i<=length/2;i++){
swap(s[i],s[length-i]);
}
}
int main(){
string str ="This is an example";
cout << "The original string is: " << str << endl;
reverseString(str);
cout << "The reversed string is: " << str << endl;
return 0;
}
#include <iostream>
using namespace std;
void reverseString(string& s){
int length = s.size() -1;
for(int i=0; i<=length/2;i++){
swap(s[i],s[length-i]);
}
}
int main(){
string str ="This is an example";
cout << "The original string is: " << str << endl;
reverseString(str);
cout << "The reversed string is: " << str << endl;
return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
char str [] = "This is example";
int i = 0,j = 0,k = 0,wordbegin=0;
for (i = 0; i <= strlen(str); i++)
{
if (str[i] == ' ' || str[i] == '\0')
{
for(j=wordbegin, k = i - 1; j<=k; j++, k--)
{
char temp = str[j];
str[j] = str[k];
str[k] = temp;
}
wordbegin = i+1;
}
}
printf ("Output: %s",str);
return 0;
}
public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}
System.out.println("Output for "+input+" is : "+sb);
}
public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}
System.out.println("Output for "+input+" is : "+sb);
}
and
public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();
for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}
System.out.println("Output for "+input+" is : "+sb);
}
and
public class ReverseString
{
public String reverseStringPlain(String sampleString)
{
char[] sampleCharArray=sampleString.toCharArray();
//System.out.println("Original String");
//displayCharArray(sampleCharArray);
char tempChar='a';
int arrayStartWalker=0;
for (int arrayEndWalker=sampleCharArray.length-1; arrayEndWalker>=sampleCharArray.length/2; arrayEndWalker--)
{
if (arrayStartWalker!=arrayEndWalker)
{
tempChar=sampleCharArray[arrayStartWalker];
sampleCharArray[arrayStartWalker]=sampleCharArray[arrayEndWalker];
sampleCharArray[arrayEndWalker]=tempChar;
}
arrayStartWalker++;
}
//System.out.println("reverse string");
//displayCharArray(sampleCharArray);
return(new String(sampleCharArray));
}
public void displayCharArray(char[] sampleCharArray)
{
for (char sampleChar : sampleCharArray)
{
System.out.print(sampleChar);
}
System.out.println("");
}
}
public class ReverseString
{
public String reverseStringPlain(String sampleString)
{
char[] sampleCharArray=sampleString.toCharArray();
//System.out.println("Original String");
//displayCharArray(sampleCharArray);
char tempChar='a';
int arrayStartWalker=0;
for (int arrayEndWalker=sampleCharArray.length-1; arrayEndWalker>=sampleCharArray.length/2; arrayEndWalker--)
{
if (arrayStartWalker!=arrayEndWalker)
{
tempChar=sampleCharArray[arrayStartWalker];
sampleCharArray[arrayStartWalker]=sampleCharArray[arrayEndWalker];
sampleCharArray[arrayEndWalker]=tempChar;
}
arrayStartWalker++;
}
//System.out.println("reverse string");
//displayCharArray(sampleCharArray);
return(new String(sampleCharArray));
}
public void displayCharArray(char[] sampleCharArray)
{
for (char sampleChar : sampleCharArray)
{
System.out.print(sampleChar);
}
System.out.println("");
}
}
A raw C based solution would be below. I am sure there might be a faster way to solve this.
//Gets to the end of the current word
int getNextToken(char *pStr) {
int nTokenLen = 0;
//"abc defg gi"
while(*pStr != '\0') {
if(*pStr == ' ') break;
nTokenLen++;
pStr++;
}
return nTokenLen;
}
//Skips the white spaces before a word
char * skipWhiteSpaces(char *pWN) {
while(*pWN != '\0') {
if(*pWN != ' ') break;
pWN++;
}
return pWN;
}
//Reverses the current word, so changes "Hello" to "olleH"
void reverseString(char *pWB, char *pWE) {
while(pWB < pWE) {
char ch = *pWB;
*pWB = *pWE;
*pWE = ch;
pWB++;
pWE--;
}
}
//pWN -> Points to the new Word ptrWordNew
//pWE -> Points to the end of the current word ptrWordEnd
//pWB -> Points to the begining of the current word ptrWordBegin
void reverseWordsInAString(char *pStr) {
char *pWB = NULL, *pWE = NULL, *pWN = NULL;
pWN = pStr;
int nNextTokenLen = getNextToken(pStr);
do {
pWB = pWE = pWN;
if(nNextTokenLen) {
pWE += (nNextTokenLen - 1); //moves to the end of word
pWN += nNextTokenLen; //moves to the next space
reverseString(pWB, pWE);
}
if(*pWN == '\0') break;
if(*(pWN + 1) == ' ')
pWN = skipWhiteSpaces(pWN);
else
pWN++;
nNextTokenLen = getNextToken(pWN);
}while(nNextTokenLen);
}
void test_CC_5694329337151488() {
char pszSentence[] = "Hello My Friend How Are You";
cout<<"Original string.."<<pszSentence<<endl;
reverseWordsInAString(pszSentence);
cout<<"Words reversed to.."<<pszSentence;
}
public static void swap (char[] arr, int i, int j)
{
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static String wordReverse(String str)
{
char[] input = str.toCharArray();
int len = str.length();
int st = 0;
int end = 0;
for (int i = 0; i < len; i++) {
if (input[i]==' '|| i+1 == len) {
end = i-1;
if (i+1 == len) {
end = i;
}
reverse(input, st, end);
st = i+1;
}
}
return new String(input);
}
public static void reverse(char[] arr, int st, int end)
{
int len = end-st+1;
for (int i = 0; i < len/2;i++ ) {
swap(arr,st+i,end-i);
}
}
public static void swap (char[] arr, int i, int j)
{
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static String wordReverse(String str)
{
char[] input = str.toCharArray();
int len = str.length();
int st = 0;
int end = 0;
for (int i = 0; i < len; i++) {
if (input[i]==' '|| i+1 == len) {
end = i-1;
if (i+1 == len) {
end = i;
}
reverse(input, st, end);
st = i+1;
}
}
return new String(input);
}
public static void reverse(char[] arr, int st, int end)
{
int len = end-st+1;
for (int i = 0; i < len/2;i++ ) {
swap(arr,st+i,end-i);
}
}
public static void swap (char[] arr, int i, int j)
{
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static String wordReverse(String str)
{
char[] input = str.toCharArray();
int len = str.length();
int st = 0;
int end = 0;
for (int i = 0; i < len; i++) {
if (input[i]==' '|| i+1 == len) {
end = i-1;
if (i+1 == len) {
end = i;
}
reverse(input, st, end);
st = i+1;
}
}
return new String(input);
}
public static void reverse(char[] arr, int st, int end)
{
int len = end-st+1;
for (int i = 0; i < len/2;i++ ) {
swap(arr,st+i,end-i);
}
}
public static String reverseString(String s){
s+=s+" ";
String myString="";
int lastIndex=0;
int lenght=0;
for(int i=0; i < s.length(); i++){
int l=0;
if(s.charAt(i)==' '){
String subString =s.substring(lastIndex, i);
lenght=(i-lastIndex);
char[] ch= new char[lenght];
l=lenght;
for(int j=0;j< lenght;j++){
l=l-1;
ch[j]=subString.charAt(l);
}
myString +=String.valueOf(ch) +" ";
lastIndex=i+1;
}
}
return myString;
}
public class ReverseString {
public static void main(String[] args) {
String str ="This is String Reverse Example ";
String s = reverseString(str);
System.out.println("Reverse String is -> "+ s);
}
public static String reverseString(String s){
s+=s+" ";
String myString="";
int lastIndex=0;
int lenght=0;
for(int i=0; i < s.length(); i++){
int l=0;
if(s.charAt(i)==' '){
String subString =s.substring(lastIndex, i);
lenght=(i-lastIndex);
char[] ch= new char[lenght];
l=lenght;
for(int j=0;j< lenght;j++){
l=l-1;
ch[j]=subString.charAt(l);
}
myString +=String.valueOf(ch) +" ";
lastIndex=i+1;
}
}
return myString;
}
}
public static String reverseString(String s){
s+=s+" ";
String myString="";
int lastIndex=0;
int lenght=0;
for(int i=0; i < s.length(); i++){
int l=0;
if(s.charAt(i)==' '){
String subString =s.substring(lastIndex, i);
lenght=(i-lastIndex);
char[] ch= new char[lenght];
l=lenght;
for(int j=0;j< lenght;j++){
l=l-1;
ch[j]=subString.charAt(l);
}
myString +=String.valueOf(ch) +" ";
lastIndex=i+1;
}
}
return myString;
}
using System;
using System.Collections.Generic;
namespace ReverseStringInSentence
{
class Program
{
static void Main(string[] args)
{
var input = "This is an example";
Console.WriteLine(ReverseStrings(input));
Console.ReadLine();
}
public static string ReverseStrings(string sentence)
{
if (sentence == null) throw new ArgumentNullException();
if (sentence == string.Empty) throw new ArgumentException("Input is an empty string");
string[] words = sentence.Split(' ');
var reveresedSentence = string.Empty;
foreach (var word in words)
{
var reversedWord = string.Empty;
var stack = new Stack<char>();
foreach (var ch in word)
{
stack.Push(ch);
}
foreach (var item in stack.ToArray())
{
reversedWord += stack.Pop();
}
reveresedSentence += string.Format("{0} ", reversedWord);
}
return reveresedSentence;
}
}
}
public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}
return newHolder.toString();
}
public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}
return newHolder.toString();
}
public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}
return newHolder.toString();
}
public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}
return newHolder.toString();
}
public static void main(String[] args) {
String str="This is an example";
String[] arr=str.split(" ");
// using StringBuilder reverse method
StringBuilder sb=new StringBuilder();
for(int i=0; i< arr.length ;i++){
sb.append(arr[i]+" ");
}
sb.reverse();
System.out.println(sb.toString());
// using StringBuilder without reverse method
StringBuilder sb1 = new StringBuilder("");
for(int i=arr.length-1; i>=0;i--){
char[] temp = arr[i].toCharArray();
String reverse = "";
for(int j = temp.length-1; j>=0 ; j--)
reverse = reverse + temp[j];
sb1.append(reverse+" ");
}
System.out.println(sb1.toString());
}
#Python Solution
string = "This is an example"
x = []
y = ""
for i in range(1, len(string)+1):
j = -1*i
y = y + string[j]
#print (y)
k = ''
for i in range(0, len(string)):
if string[i] != ' ':
k = k +string[i]
if (string[i] == ' ') or (i == (len(string)-1)):
x.append(k)
k = ''
#print (x)
z = []
q = ''
for j in range(0, len(x)):
t = str(x[j])
for p in range(1,len(t)+1):
p = -1*p
q = q + t[p]
z.append(q)
q = ''
#print (z)
rev_str = ''
for g in range(0,len(z)):
rev_str = rev_str + ' ' + str(z[g])
print (rev_str.lstrip())
string = "This is an example"
x = []
y = ""
for i in range(1, len(string)+1):
j = -1*i
y = y + string[j]
#print (y)
k = ''
for i in range(0, len(string)):
if string[i] != ' ':
k = k +string[i]
if (string[i] == ' ') or (i == (len(string)-1)):
x.append(k)
k = ''
#print (x)
z = []
q = ''
for j in range(0, len(x)):
t = str(x[j])
for p in range(1,len(t)+1):
p = -1*p
q = q + t[p]
z.append(q)
q = ''
#print (z)
rev_str = ''
for g in range(0,len(z)):
rev_str = rev_str + ' ' + str(z[g])
print (rev_str.lstrip())
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String sent = sc.nextLine();
ReverseString rs = new ReverseString();
rs.stringReverse(sent);
}
private void stringReverse(String str){
boolean flag=true;
int beginIndex=0;
int endIndex=0;
char[] strArr = str.toCharArray();
int i=0;
while(flag){
if(strArr[i+1]!=' ' || strArr[i+1]==strArr.length-1){
endIndex++;
i++;
if(i==str.length()-1){
printString(strArr, beginIndex, endIndex);
flag=false;
}
}
else{
printString(strArr, beginIndex, endIndex);
endIndex++;
beginIndex=endIndex+1;
i++;
}
}
}
private void printString(char[] strArr, int beginIndex, int endIndex) {
// TODO Auto-generated method stub
for(int j=endIndex; j>=beginIndex; j--){
System.out.print(strArr[j]);
}
System.out.print(" ");
}
}
public class WordReverse
{
public static void main(String[] args)
{
String str ="This is me.., i'm the boss";
String[] words = str.split(" ");
List al = new ArrayList();
for (int i = 0; i < words.length; i++)
{
al.add(strRrev(words[i]) + " ");
System.out.print(al.get(i));
}
}
public static String strRrev(String str)
{
String temp = "";
for (int i = str.length()-1; i>=0 ; i--)
{
temp = temp + str.charAt(i);
}
return temp;
}
}
/**
* Javascript Implementation.
* reverseStringInPlace
Write program for the following case
Reverse string (string is stored in an array)
Input:- "This is an example"
Output:-sihT si na elpmaxe
*/
function reverseStringInPlace(str) {
let words = str.split(' ');
for(let index = 0, length = words.length; index < length; index++) {
words[index] = words[index].split('').reverse().join('');
}
return words.join(' ');
}
let str = 'This is an example';
console.log("reverseStringInPlace ->");
console.log("Input : " + str);
console.log("Output : " + reverseStringInPlace(str));
public static String reverseGivenString(String input) {
String strTemp = "";
for(int i=input.length()-1; i>=0; i--) {
strTemp += "" + input.charAt(i);
}
return strTemp;
}
public static void main(String[] args) {
String str = "This is test program";
for(String s:str.split(" ")) {
System.out.print(reverseGivenString(s) + " ");
}
}
- tus124 July 26, 2015