Agilent Technologies Interview Question
Software Engineer / DevelopersI would do one of the following:
1. Scan the sentence, at each word break, pull out the word and push it on a stack. Once the end of the sentence is reached, pop off each word from the stack and print it out. This will give you the last word first, 2nd to last word, etc.
Or
2. Get a pointer to the end of the sentence. From the end, work backward looking for word breaks. When you find one, print from this position to the last word break (or previous work break).
I think #1 because you only scan the sentence once.
I would do one of the following:
1. Scan the sentence, at each word break, pull out the word and push it on a stack. Once the end of the sentence is reached, pop off each word from the stack and print it out. This will give you the last word first, 2nd to last word, etc.
Or
2. Get a pointer to the end of the sentence. From the end, work backward looking for word breaks. When you find one, print from this position to the last word break (or previous work break).
I think #1 will be faster because you only scan the sentence once. (I'm thinking faster than I'm typing)
Hey Dhaval, how is that program going to print the words in reverse. It's already printing "s'taht" in the second inner loop. I agree that implementing a stack or anything related to the idea of a stack would work. i haven't programmed in like 3 years and I'm not familiar with c++ either, but for a non-stack implementation without a buffer try this.
void main(){
char* c="That's going to be crazy.";
int word[20];
int i = 0;
word[0]=0;
int w = 1;
while(c[i]!='\0'){
if(c[i]==' '){
word[w] = i+1;
w++;
}
i++;
}
//word[0]=0, word[1]=7,word[2]=13,word[3]=16,word[4]=19
//w=5,i=25 now, do you see that.
//thus i-1 is 24 right and c[24]contains the period '.'
int p;
for(int j=w;j>0;j--){
p=word[j-1];
while(p<i){
printf("%c",c[p++]);
}
i=word[j-1]-1;
//after first iteration i=18
}
}//end main()
can be done using two stacks
as u scan the sentence keep pushing untill u get a space when u do get a space pop all the elements from first stack and push them in to the second stack and keep doing it like this...
finally pop from the second stack all the elements...
u have the words in reverse order
#include "stdio.h"
void ReverseString(char str[], int start, int end)
{
char temp;
while (start < end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++; end--;
}
return;
}
void ReverseWords(char str[])
{
int start = 0, end = 0, length;
length = strlen(str);
ReverseString(str, start, length-1);
while (end < length)
{
if (str[end] != ' ')
{
start = end;
while (end < length && str[end] != ' ')
{
end++;
}
end--;
ReverseString(str, start, end);
}
end++;
}
return;
}
int main()
{
char str[]="That's going to be crazy.";
ReverseWords(str);
printf("%s\n", str);
}
hmm didnt post properly earlier
void printrev(char* s) {
for (int i=strlen(s);i>=0;i--)
if (s[i]==' ') {
printf("%s ", s+i+1)
s[i] = (char)0;
}
printf("%s\n", s);
}
void ReverseWords (char str[])
{
int start = 0, end = 0, length;
length = strlen(str);
/* Reverse entire string */ ReverseString(str, start, length - 1);
while (end < length) {
if (str[end] != ' ') { /* Skip non-word characters */
/* Save position of beginning of word */ start = end;
/* Scan to next non-word character */ while (end < length && str[end] != ' ') end++;
/* Back up to end of word */ end--;
/* Reverse word */ ReverseString(str, start, end);
}
end++; /* Advance to next token */
}
return; }
void ReverseString (char str[], int start, int end) { char temp; while (end > start) {
/* Exchange characters */
temp = str [start];
str [start] = str [end] ;
str [end] = temp;
/* Move indices towards middle */ start++; end--;
} return;
}
void printstr(char *srgs, int pos){
- AJ April 03, 2006char temp[20];
int idx=0;
while( (srgs[pos] != ' ') && (srgs[pos] != '\0')){
temp[idx++] = srgs[pos];
pos++;
}
if (srgs[pos] == '\0'){
temp[idx]= '\0';
printf ("%s %d \n", temp, pos);
return;
}
else
printstr(srgs, pos+1);
temp[idx]= '\0';
printf ("%s ", temp);
}