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 can a program be made to print the name of a source file where an error occurs?

656


What is a stream?

623


Explain how does free() know explain how much memory to release?

550


Sir i need notes for structure,functions,pointers in c language can you help me please

1919


What is a memory leak? How to avoid it?

537






while initialization of array why we use a[][2] why not a[2][]...?

1836


Is c programming hard?

553


what is the diffrenet bettwen HTTP and internet protocol

1362


Explain the difference between getch() and getche() in c?

542


Is array name a pointer?

581


why do some people write if(0 == x) instead of if(x == 0)?

629


When can a far pointer be used?

562


Are pointers integers in c?

586


What is the difference between if else and switchstatement

1285


How will you print TATA alone from TATA POWER using string copy and concate commands in C?

890