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?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain how are 16- and 32-bit numbers stored?

789


Explain how do you list a file’s date and time?

622


What is memcpy() function?

626


How can I discover how many arguments a function was actually called with?

637


What is the use of #define preprocessor in c?

623






What are the header files used in c language?

595


Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.

709


Explain Function Pointer?

686


What is wrong with this program statement? void = 10;

828


What are the 32 keywords in c?

639


What are the __date__ and __time__ preprocessor commands?

579


pgm to find any error in linklist(in single linklist check whether any node points any of previous nodes instead of next node)

2206


Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. z-a:zyx......ba -1-6-:-123456- 1-9-1:123456789987654321 a-R-L:a-R...L a-b-c:abbc

5065


Explain what are header files and explain what are its uses in c programming?

633


What are structural members?

576