"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 16 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the data types present in c?

629


Tell me the use of bit field in c language?

629


In C, What is the #line used for?

1060


Is it better to use malloc() or calloc()?

649


How can I invoke another program or command and trap its output?

617






Q.1 write aprogram to stack using linklist o insert 40 items? Q.2 write a program to implement circular queue with help of linklist?

1600


Explain how can I right-justify a string?

625


What does & mean in scanf?

604


What is console in c language?

608


What is identifiers in c with examples?

675


Add Two Numbers Without Using the Addition Operator

353


What is the code in while loop that returns the output of given code?

1315


How do you view the path?

670


Linked lists -- can you tell me how to check whether a linked list is circular?

646


What is nested structure?

573