Skip to content

Instantly share code, notes, and snippets.

@laughingclouds
Created December 9, 2021 09:45
Show Gist options
  • Save laughingclouds/84f9a61be1e91e6af2a214310ab4bbad to your computer and use it in GitHub Desktop.
Save laughingclouds/84f9a61be1e91e6af2a214310ab4bbad to your computer and use it in GitHub Desktop.
Reverse a string in C
#include <stdio.h>
#include <string.h>
void strrev(char *str, char *nStr) {
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
strncat(nStr, &str[i], 1);
}
}
int main() {
char str[100];
printf("Enter string: ");
scanf("%s", str);
int len = strlen(str);
char nStr[100];
strrev(str, nStr);
printf("%s\n", nStr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment