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
Reverse a String in C
Reverse a String in C
This C program reverses a given string using a simple two-pointer technique. It swaps characters from both ends until the middle is reached.
✅ C Program Code:
#include <stdio.h>
#include <string.h>
void reverse(char str[])
{
int start = 0;
int end = strlen(str) - 1;
int temp;
while (start < end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main()
{
char str[100];
printf("Enter the string:\n");
scanf("%[^\n]", str);
printf("Before reversing the string: %s\n", str);
reverse(str);
printf("After reversing the string: %s", str);
}
๐ How It Works:
- reverse(): Swaps characters from start and end using a while loop until the full string is reversed.
- scanf("%[^\n]", str): Reads a line of input including spaces.
- main(): Takes user input, prints original and reversed string.
๐ป Sample Output:
Hello World
Before reversing the string: Hello World
After reversing the string: dlroW olleH
๐ Keywords:
reverse string in C, string handling in C, two-pointer approach C, beginner string program, C logic examples, character array reverse
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