Skip to content

Commit

Permalink
Merge pull request WebClub-NITK#400 from incinirate/patch-1
Browse files Browse the repository at this point in the history
Add string reverse implementation
  • Loading branch information
sbshah97 authored Oct 14, 2017
2 parents e6e7cb9 + c446c0f commit ab05ed7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Reverse_String/incin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>

#define MAX_STRING 256

char *strrev(char *str) {
char *p1, *p2;

// Ensure the string exists and has data
if (! str || ! *str)
return str;

// Flip the characters on the ends of the string, progressing inward towards the string center
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}

return str;
}

void main() {
char str[MAX_STRING + 2]; // Two extra bytes, one for the newline (\n) and one for the NULL terminator

printf("\nEnter String: ");
fgets(str, MAX_STRING + 2, stdin);

str[strlen(str) - 1] = (char)NULL;

printf("Original: %s\n", str);
printf("Reversed: %s\n", strrev(str));

return 0;
}

0 comments on commit ab05ed7

Please sign in to comment.