Created
December 3, 2021 16:52
-
-
Save evilprince2009/9568f08f1e4cf651d3ea581800cb6df4 to your computer and use it in GitHub Desktop.
Reverse string in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program is divided into two smaller parts so you can see how the | |
// things actually work. | |
#include <stdio.h> | |
int strlen(char *s) | |
{ | |
int i = 0; | |
while (*s++) | |
i++; | |
return i; | |
} | |
char *reverse(char *str) | |
{ | |
int front, rear; | |
for (front = 0, rear = strlen(str) - 1; front < rear; front++, rear--) { | |
char tmp = str[front]; | |
str[front] = str[rear]; | |
str[rear] = tmp; | |
} | |
return str; | |
} | |
int main() | |
{ | |
char str[] = "JQR"; | |
printf("%s\n", reverse(str)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment