Q13. Write a C program to reverse a word.

Also interviewer can ask  to write a C program to reverse a string. In below program we give input “welcome to gyantoday” Then output will be “yadotnayg ot emoclew” .

Lets reverse “gyantoday”

#include<stdio.h>

#include<string.h>

void reverse_word(char *str)

{

int i, length;

char temp;

length = strlen(str);

for (i = 0; i < length/2; i++)

{

temp = *(str+i);

*(str+i) = *(str+length-i-1);

*(str+length-i-1) = temp;

}

}

int main()

{

char word[] = “gyantoday”;

reverse_word(word);

printf(“%s\n”,word);

return 0;

}

Output:

yadotnayg

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.