In: Computer Science
Use C to Write a program that takes the array reverse_me and reverses the order of the elements (i.e., the first element and the last element are swapped, the second element and the second-to-last element are swapped, etc.). Store the reversed array in reversed_arr. Finally, print out the reversed array as a string.
C PROGRAM
#include<stdio.h>
#include<string.h>
#include<malloc.h>
// implement reverse_me() function with two arguments
// first argument pointer with character variable str
// second argument integer n for size of str
void reverse_me(char *str,int n)
{
int i; // declare integer variable i
char temp; // declare character variable temp
for(i=0;i<n/2;i++) // create for loop until n/2
times
{
// swapping of characters
temp=str[i];
str[i]=str[n-i-1];
str[n-i-1]=temp;
}
}
int main()
{
char *s; // declare pointer with character variable
s
int n; // declare integer variable n
// dynamic memory allocation of pointer s
variable
s=(char *)malloc(sizeof(char));
printf("Enter a String: ");
scanf("%s",s); // read string
n=strlen(s); // calculate lenght of s string
reverse_me(s,n); // calling reverse_me()
function
printf("Reverse String: %s",s); // display reverse
string
}
OUTPUT-1
Enter a String: cprogramming
Reverse String: gnimmargorpc
OUTPUT-2
Enter a String: JavaProgramming
Reverse String: gnimmargorPavaJ