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
Do array subscripts always start with zero?
What is void main ()?
Explain what is the best way to comment out a section of code that contains comments?
What do you mean by a sequential access file?
Does c have an equivalent to pascals with statement?
What is the return type of sizeof?
What are the string functions? List some string functions available in c.
What is wrong in this statement?
Tell me can the size of an array be declared at runtime?
write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.
How do I create a directory? How do I remove a directory (and its contents)?
Write a program of prime number using recursion.
Difference between Shallow copy and Deep copy?
What is sizeof array in c?
in programming languages a statement or part of a statement that specifies several different execution sequences a) constructs b) distructs c) executes d) none