Given an array of characters which form a sentence of
words, give an efficient algorithm to reverse the order of
the words (not characters) in it?

Answers were Sorted based on User's Feedback



Given an array of characters which form a sentence of words, give an efficient algorithm to revers..

Answer / tarak

#include<stdio.h>
int main()
{
char *p="i am working in TechM";
char *s,*temp;
char a[20];
int i=0;
s=p;
while(*p != '\0')
p++;
while(s != p){
while(*(--p)!=' ');
temp=p;
p++;
while(*p != '\0' && *p != ' ')
{
a[i++]=*p;
p++;
}
a[i++]=' ';
p=temp;
p--;
}
while(*s != ' ')
a[i++]=*s++;
a[i] = '\0';
printf("%s \n",a);
}
~

Is This Answer Correct ?    3 Yes 0 No

Given an array of characters which form a sentence of words, give an efficient algorithm to revers..

Answer / anubhav meena

#include <stdafx.h>
#include <string.h>

char* ReverseString(char *a){

int length = strlen(a);
printf("length is:%d\n",length);
int i=0;
int j=length-1;
while(i<j){
*(a+i)=*(a+i)^*(a+j);
*(a+j)=*(a+i)^*(a+j);
*(a+i)=*(a+i)^*(a+j);
i++;
j--;
}

return a;
}

int main()
{
char s[] = "katrina kaif is gorgeous gal!";
char *a=s;
char *b=s;
char *e= a+strlen(a);
printf("String is:%s\n",a);
ReverseString(a);
printf("NewString is:%s\n",a);
int i=0;
while(*(a+i)!='\0'){
while(*(a+i)!=' ' && *(a+i)!='\0') i++;
*(a+i)='\0';
ReverseString(a);

if((a+i)!=e){
*(a+i)=' ';
a = a+i+1;
}
else{
break;
}
i=0;
}
printf("FinalString is:%s\n",b);
getchar();

Is This Answer Correct ?    3 Yes 0 No

Given an array of characters which form a sentence of words, give an efficient algorithm to revers..

Answer / abdur rab

#include <stdio.h>

void reverse_string ( char* cp_string, int start, int end )
{
if ( cp_string && ( start < end ) ) {
*( cp_string + start ) ^= *( cp_string +
end )
^= *( cp_string + start ) ^= *(
cp_string + end );
reverse_string ( cp_string, ++start, --
end );
}
}


int main ( int argc, char* argv [] )
{
char ca_array [12]={"Hello World"};
char* cp_ptr = NULL;
char* cp_ptr_temp = NULL;

printf ( "\n Before Reverse :%s", ca_array );

reverse_string ( ca_array, 0, strlen ( ca_array ) -
1 );

cp_ptr_temp = cp_ptr = ca_array;
while ( NULL != ( cp_ptr_temp = (char*) strchr (
cp_ptr_temp, ' ' ) ) ) {
reverse_string ( cp_ptr, 0, ( cp_ptr_temp -
cp_ptr ) - 1 );
cp_ptr = ++cp_ptr_temp;
}
// change the final word
reverse_string ( cp_ptr, 0,
( ( strlen ( ca_array ) - 1 ) - (
cp_ptr - ca_array ) ) );

printf ( "\n After Reverse by Words :%s",
ca_array );
return ( 0 );
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Interview Questions

How many data structures are there in c?

0 Answers  


find largest of 3 no

8 Answers  


What are the different properties of variable number of arguments?

0 Answers  


Does * p ++ increment p or what it points to?

0 Answers  


What is the scope of static variables?

1 Answers  






How macro execution is faster than function ?

0 Answers   Wipro,


7. Identify the correct argument for the function call fflush() in ANSI C: A)stdout B)stdin C)stderr D)All the above

10 Answers   Accenture,


Which is not valid in C? 1) class aClass{public:int x;} 2) /* A comment */ 3) char x=12;

7 Answers  


What is a file descriptor in c?

0 Answers  


6)What would be the output? main() { int u=1,v=3; pf("%d%d",u,v); funct1(&u,&v); pf("%d%d\n",u,v); } void funct1(int *pu, int *pv) { *pu=0; *pv=0; return; } a)1 3 1 3 b)1 3 1 1 c)1 3 0 0 d)1 1 1 1 e) 3 1 3 1

4 Answers  


What is memmove?

1 Answers   Oracle,


what is meant by c

9 Answers   INiTS,


Categories