"I LOVE MY COUNTRY"
write a c program to get "COUNTRY MY LOVE I" as the output.
Use any other programming language. It is not mandatory to
use C.
Answer Posted / sandeep
#include <stdio.h>
void rev(char *l, char *r);
int main(int argc, char *argv[])
{
char buf[] = "I LOVE MY COUNTRY";
char *end, *x, *y;
// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);
// Now swap each word within sentence...
x = buf-1;
y = buf;
while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}
// Now print the final string....
printf("%s\n",buf);
return(0);
}
// Function to reverse a string in place...
void rev(char *l,char *r)
{
char t;
while(l < r)
{
t = *l;
*l++ = *r;
*r-- = t;
}
}
| Is This Answer Correct ? | 35 Yes | 17 No |
Post New Answer View All Answers
What are pointers? What are stacks and queues?
Write a function stroverlap that takes (at least) two strings, and concatenates them, but does not duplicate any overlap. You only need to worry about overlaps between the end of the first string and the beginning of the second string. Examples: batman, manonthemoon = batmanonthemoon batmmamaman, mamamanonthemoon = batmmamamanonthemoon bat, man = batman batman, batman = batman batman, menonthemoon = batmanmenonthemoon
What is use of integral promotions in c?
find the value of y y = 1.5x+3 for x<=2 y = 2x+5 for x>2
Differentiate between the = symbol and == symbol?
Explain what is the best way to comment out a section of code that contains comments?
What is signed and unsigned?
How can I remove the leading spaces from a string?
swap 2 numbers without using third variable?
What is the general form of #line preprocessor?
What are the various types of control structures in programming?
What is huge pointer in c?
Why static is used in c?
Differentiate between static and dynamic modeling.
What is the difference between typedef and #define?