a program in c with detail explanation to dynamically allocate memory for a string and reverse the string in place.

Program Step by Step Description
1.Use malloc() function characteristic to dynamically allocate memory for a string.
2.Read the enter string from the user using scanf() or fgets() function.
3.Use strlen() string function to calculate the length of the string.
4.Use two pointers to swap or exchange a characters from the beginning to the end of the string till they meet in the center.
5.Use printf() to display the reversed string.
Real Life Applications
1.Word or text processing applications where string manipulation is required.
2.Data encryption algorithms that contain reversed strings.
3.Implementation of data systems such as stacks and queues where string reversals may be important.
4.Development of palindrome checking algorithms in which string flipping or reversing is a key step.
5.Implementation of sorting algorithms that include string reversals as part of the procedure.


C program that dynamically allocates memory for a string and reverses the string in place.

#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { char *inputString; int i,j,temp; // Dynamically allocate memory for the string inputString=(char *)malloc(100*sizeof(char)); if(inputString==NULL) { printf("\nMemory allocation failed. Exiting program."); return 1; } // Read input string from the user printf("\nEnter a string: "); scanf("%s", inputString); // Reverse the string in place j=strlen(inputString) - 1; for(i=0;i< j;i++,j--) { temp=inputString[i]; inputString[i]=inputString[j]; inputString[j]=temp; } // Display the reversed string printf("\nReversed string: %s\n", inputString); // Free dynamically allocated memory free(inputString); return 0; }

Output
Enter a string: Sankalandtech
Reversed string: hcetdnalaknaS


Program Explanation
1.The code dynamically allocates memory for a string with the help of malloc() function.
2.It reads an entered string from the consumer or user.
3.It reverses the string in location through swapping characters from the beginning and to the end of the string.
4.The reversed string is then shown to the consumer using printf().
5.Finally the dynamically allotted memory is freed using free() function to avoid memory leaks problem.


Previous :-->> 1. Write a program in C programming to dynamically allocate memory for an array of integers and display the sum of all elements.
 -->> NEXT: 3. Write a C language program that dynamically allocates memory for a 2D array and calculates the transpose of the matrix.
-->>ALL Dynamic Memory Allocation assignments in c