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 optimization in c?
Is this program statement valid? INT = 10.50;
2) Write a program that will help Air Traffic Control for an airport to view the sequence of flights ready for take-off. The airport can accommodate 10 flights waiting for take-off at any point in time. Each flight has a unique 3 digit numeric identifier. Each time a flight takes-off, Air Traffic Control adds a flight to the waitlist. Each time a flight is added to the waitlist, the list of flights waiting to take-off must be displayed. When a flight is cleared for take-off, Air Traffic Control removes the flight from the waitlist. Each time a flight takes-off, the list of flights waiting to take-off must be displayed. Sequence of take-off is the sequence of addition to the waitlist
Why is a semicolon (;) put at the end of every program statement?
A collection of data with a given structure for excepting storing and providing on demand data for multiple users a) linked list b) datastructer c) database d) preprocessor
Please send me WIPRO technical question to my mail ID.. its nisha_g28@yahoo.com please its urgent
What are derived data types in c?
Define C in your own Language.
Is javascript based on c?
What is the right way to use errno?
Explain the meaning of keyword 'extern' in a function declaration.
What are all different types of pointers in c?
Explain output of printf("Hello World"-'A'+'B'); ?
What does c mean?
what are the advanced features of functions a) function declaration and prototypes b) calling functions by value or by reference c) recursion d) all the above