Agilent Technologies Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

void printstr(char *srgs, int pos){
char 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);



}

- AJ April 03, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 because you only scan the sentence once.

- Ryan May 01, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Ryan May 01, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
char* c="That's going to be crazy";
char buff[100];

while(*c!='\0')
{
int i=0;
while( (*c!='\0') && (*c!=' '))
{
buff[i++]=*c;
c++;
}

if((*c==' ') || (*c=='\0'))
{
i--;
while(i>=0)
printf("%c",buff[i--]);
printf(" ");
c++;
}

}

}

- Dhaval May 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, Krammer, is it allowed use strtok() to get the words?

- Kevin May 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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()

- MFChow May 20, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Manu May 24, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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);
}

- someone May 27, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's a recursive function. No need for any explicit stacks now. Ha Ha :)

void printRev(char *s)
{
int cnt=0; char *tmp = s;
for( ; *tmp!=' ' && *tmp!='\0'; ++tmp, ++cnt);
if( *tmp == ' ' )
printRev(tmp+1);
char fs[15];
sprintf(fs, "%%.%ds ", cnt);
printf(fs, s);
}

- creator June 12, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

easier...

void printrev(char* s) {
for (int i=strlen(s);i>=0;i++)
if (s[i]==' ') {
printf("%s", s)

printf("%s", s);
}

- zunk October 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

- zunk October 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try surrounding your code with "{ { {" and "} } }" (no spaces though :-) ).

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);
  }
}

- Gayle L McDowell December 28, 2006 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Java Coder November 26, 2007 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More