Skip to content

Instantly share code, notes, and snippets.

@evilprince2009
Created December 3, 2021 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilprince2009/9568f08f1e4cf651d3ea581800cb6df4 to your computer and use it in GitHub Desktop.
Save evilprince2009/9568f08f1e4cf651d3ea581800cb6df4 to your computer and use it in GitHub Desktop.
Reverse string in C
// 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