In: Computer Science
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output:
$ ./program.exe
enter string: nvidia
before: nvidia
after: aidivn
in C
Hello learner,
Thanks for asking.
Here is the C programming code to reverse a string.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
char str[80];
char rev[80];
printf("enter string:\t");
scanf("%s", str);
printf("before : %s\n", str);
// for loop is used to reverse the caracters //
for(i = 0; str[i] != '\0'; i++);
{
k = i-1;
}
for(j = 0; j <= i-1; j++)
{
rev[j] = str[k];
k--;
}
printf("after : %s\n", rev);
getch();
}
In this code at first input is taken from the user and then store it as a string type. After that by using for loop each and every caracter of the string is reversed one by one to the rev[ ] from str[ ]. when the loop is completed then reverse string is printed.
Here is the image of code with proper output.
use proper indentation for less error.
Please upvote and ask if you have any query.