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
What is #include conio h?
How can I trap or ignore keyboard interrupts like control-c?
How can I swap two values without using a temporary?
What type of function is main ()?
What is a program?
What are qualifiers and modifiers c?
Can we change the value of constant variable in c?
Is file a keyword in c?
How can a number be converted to a string?
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.
What is malloc() function?
What is difference between structure and union in c programming?
Why c is faster than c++?
how many key words availabel in c a) 28 b) 31 c) 32
How #define works?