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 / tarak
#include<stdio.h>
int main()
{
char *p="i am working in TechM";
char *s,*temp;
char a[20];
int i=0;
s=p;
while(*p != '\0')
p++;
while(s != p){
while(*(--p)!=' ');
temp=p;
p++;
while(*p != '\0' && *p != ' ')
{
a[i++]=*p;
p++;
}
a[i++]=' ';
p=temp;
p--;
}
while(*s != ' ')
a[i++]=*s++;
a[i] = '\0';
printf("%s \n",a);
}
~
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
What is multidimensional arrays
What is meant by keywords in c?
What is the purpose of sprintf?
Find duplicates in a file containing 6 digit number (like uid) in O (n) time.
What is dynamic memory allocation?
What is array of pointers to string?
Explain how do you declare an array that will hold more than 64kb of data?
Explain the difference between malloc() and calloc() function?
Which is better between malloc and calloc?
what is the function of pragma directive in c?
What is difference between constant pointer and constant variable?
What is typedef struct in c?
what are the advantages of a macro over a function?
What is LINKED LIST? How can you access the last element in a linked list?
Combinations of fibanocci prime series