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 are header files in c?
Explain what is the purpose of "extern" keyword in a function declaration?
What is the size of empty structure in c?
What is the use of sizeof?
When should we use pointers in a c program?
What is the difference between typedef struct and struct?
What do you understand by normalization of pointers?
What is pointer to pointer in c with example?
code for quick sort?
Is printf a keyword?
Distinguish between actual and formal arguments.
how to find binary of number?
Synonymous with pointer array a) character array b) ragged array c) multiple array d) none
what is stack , heap ,code segment,and data segment
What is queue in c?