Question

In: Computer Science

Use C to Write a program that takes the array reverse_me and reverses the order of...

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.

Solutions

Expert Solution

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


Related Solutions

in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT