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 can a program be made to print the name of a source file where an error occurs?
What is a stream?
Explain how does free() know explain how much memory to release?
Sir i need notes for structure,functions,pointers in c language can you help me please
What is a memory leak? How to avoid it?
while initialization of array why we use a[][2] why not a[2][]...?
Is c programming hard?
what is the diffrenet bettwen HTTP and internet protocol
Explain the difference between getch() and getche() in c?
Is array name a pointer?
why do some people write if(0 == x) instead of if(x == 0)?
When can a far pointer be used?
Are pointers integers in c?
What is the difference between if else and switchstatement
How will you print TATA alone from TATA POWER using string copy and concate commands in C?