Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! ๐ ๐นLearn advanced coding techniques in C& C++ ๐นMaster Linux internals & shell scripting ๐นDeep dive into Data Structures & Algorithms ๐นExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) ๐นGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Recursive String Reversal in C
✅ Recursive String Reversal in C
#include <stdio.h>
#include <string.h>
// Recursive function to reverse the string
void reverseString(char str[], int start, int end) {
if (start >= end)
return;
// Swap characters
char temp = str[start];
str[start] = str[end];
str[end] = temp;
// Recur for next pair
reverseString(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string:\n");
scanf(" %[^\n]", str); // Read string with spaces
printf("Original String: %s\n", str);
reverseString(str, 0, strlen(str) - 1);
printf("Reversed String: %s\n", str);
return 0;
}
๐ Explanation:
This program demonstrates how to reverse a string using a recursive approach:
- It defines a recursive function
reverseString()that swaps characters from the beginning and end, moving toward the center. - Base case: if start >= end, the function returns.
- Each recursive call handles the next inner pair of characters.
scanf(" %[^\n]", str)reads input including spaces.
๐งพ Sample Output:
Enter a string: hello world Original String: hello world Reversed String: dlrow olleh
๐ Keywords:
Recursion in C, reverse string recursively, string functions in C, string reverse logic, string manipulation, reverse using function
๐ Hashtags:
#CProgramming #Recursion #StringReversal #BeginnerC #CodeWithC #StringLogic
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment