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
Why string is used in c?
Explain what are the advantages and disadvantages of a heap?
If errno contains a nonzero number, is there an error?
What is null in c?
What are derived data types in c?
Can a pointer be volatile in c?
Explain the meaning of keyword 'extern' in a function declaration.
What is p in text message?
What is the difference between the local variable and global variable in c?
What does the file stdio.h contain?
how logic is used
What is difference between union All statement and Union?
Is array name a pointer?
hi... can anyone help me to make a two-dimensinal arrays in finding the sum of two elements plzzz. thnx a lot...
What is fflush() function?