Labs247 Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: In-Person
public class ReverseString{
public static void main(String[] arg)
{
String str = "hello my name is";
String[] arr = str.split(" ");
for(int i=(arr.length-1); i>=0; i--)
{
System.out.print(arr[i]);
System.out.print(" ");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package results;
import java.util.*;
public class Main {
Stack st;
StringTokenizer strToken;
Main(){
st = new Stack();
}
public void reverseSentence(String str){
strToken = new StringTokenizer(str);
int count = strToken.countTokens();
while (strToken.hasMoreTokens()){
st.push(strToken.nextToken());
}
for ( int i =0; i < count; i++){
System.out.println(st.pop());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main o = new Main();
o.reverseSentence("Hello my name is");
}
}
As this interview was in Java, I have given solution using string tokenizer. It tokenizes string into words, we push them on a stack and then we pop all the words (tokens ) out.
If you want to watch the video about how to reverse the words of a string or sentence please visit the link
youtu.be/WCaigw6Bz1M
#include<stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
void reverse(char *begin, char *end);
/*Function to reverse words*/
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp-1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/* Driver function to test above functions */
int main()
{
char s[] = "i like this program very much";
char *temp = s;
reverseWords(s);
printf("%s", s);
getchar();
return 0;
}
Time Complexity: O(n)
/* reverse a string. "hello my name is" to be displayed as "is name my hello" */
// WORKING CODE || RECURSIVE
#include<stdio.h>
#include<string.h>
int reverse( char C[25])
{
int i;
i=strlen(C);
if(!i)
return 0;
for(i=i-1;i>=0&&C[i]!=' ';i--);
if(i>=0)
C[i]=NULL;
printf("%s ",C+i+1);
if(i>=0)
reverse(C);
return 0;
}
int main()
{
char S[25];
gets(S);
reverse(S);
return 0;
}
Another better method is first reverse the complete sentence and then tokenzie the reverse string and reverse the tokens. Let me explain with examples
hello my name is
1. reverse whole sentence
si eman ym olleh
2. tokenize this reverse string and then reverse the tokens in the same order of words
si=is eman = name ym= my olleh= hello
hello my name is
public void reverseSentence2(String str){
char rev[]= new char[str.length()];
for ( int i =0; i < str.length(); i++){
rev[str.length()-i-1]=str.charAt(i);
}
String revStr = new String(rev);
StringTokenizer token = new StringTokenizer(revStr);
while ( token.hasMoreTokens()){
String temp = token.nextToken();
for ( int i = temp.length()-1; i>=0; i--){
System.out.printf("%c", temp.charAt(i));
}
System.out.println(" ");
}
}
#include<stdio.h>
/* function prototype for utility function to
reverse a string from begin to end */
void reverse(char *begin, char *end);
/*Function to reverse words*/
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
/*STEP 1 of the above algorithm */
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
/*STEP 2 of the above algorithm */
reverse(s, temp-1);
}
/* UTILITY FUNCTIONS */
/*Function to reverse any sequence starting with pointer
begin and ending with pointer end */
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/* Driver function to test above functions */
int main()
{
char s[] = "i like this program very much";
char *temp = s;
reverseWords(s);
printf("%s", s);
getchar();
return 0;
}
Time Complexity: O(n)
string ReverseWords(char* str)
{
if (strlen(str) <= 0) return NULL;
char *temp = new char[strlen(str) + 1];
strcpy(temp, str);
stack <char*> s;
cout << "1" << endl;
char *tok = strtok(temp, " ");
cout << "1" << endl;
while (tok != NULL)
{
s.push(tok);
s.push(" ");
cout << tok << endl;
tok = strtok(NULL, " ");
}
s.pop();
// Now get back the reverser string
string revStr;
while (s.size() != 0)
{
revStr += s.top();
s.pop();
}
delete temp;
return revStr;
}
In java using String and array only.
public static void main(String[] args) {
// TODO code application logic here
String str = "hello my name is";
String str1="";
int count =0;
String[] reverse = new String[10];
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ')
count++;
}
int c = count;
for(int i=0;i<str.length();i++){
if(str.charAt(i)!=' '){
str1 = str1+str.charAt(i);
}
if(str.charAt(i)==' '){
reverse[--count] = str1;
str1="";
}
}
for(int i=0;i<c;i++)
str1 = str1.concat(" "+reverse[i]);
System.out.println(str1);
}
import java.util.*;
public class rev
{
Stack stk;
rev()
{
stk=new Stack();
}
public void reverseStr(String s)
{
int k=0;
String r="";
char arr[]=s.toCharArray();
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=' ')
{
String x=arr[i]+"";
r=r.concat(x);
}
else
{
stk.push(r);
r="";
}
}
stk.push(r);
while(!stk.isEmpty())
{
System.out.print(stk.pop() +" ");
}
}
public static void main(String arg[])
{
rev r=new rev();
r.reverseStr("my name is sweta");
}}
public class ReverseString {
private static final String TOKEN = " ";
public static void main(String[] args) {
String input = "Fast and Furious";
String[] strings = input.split(TOKEN);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
sb.append(strings[strings.length - i - 1]).append(TOKEN);
}
System.out.println(sb.substring(0, sb.length() - 1));
}
}
- Vir Pratap Uttam May 12, 2015