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
Explain how are 16- and 32-bit numbers stored?
Explain how do you list a file’s date and time?
What is memcpy() function?
How can I discover how many arguments a function was actually called with?
What is the use of #define preprocessor in c?
What are the header files used in c language?
Write a program, where i have a grid with many cells, how many paths are possible from one point to other desired points.
Explain Function Pointer?
What is wrong with this program statement? void = 10;
What are the 32 keywords in c?
What are the __date__ and __time__ preprocessor commands?
pgm to find any error in linklist(in single linklist check whether any node points any of previous nodes instead of next node)
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
Explain what are header files and explain what are its uses in c programming?
What are structural members?