Skip to content

Instantly share code, notes, and snippets.

@0x8BADFOOD
Last active November 7, 2015 02:34
Show Gist options
  • Save 0x8BADFOOD/7555e20351b7cb1265f2 to your computer and use it in GitHub Desktop.
Save 0x8BADFOOD/7555e20351b7cb1265f2 to your computer and use it in GitHub Desktop.
Inverting string in plain c
#include <stdio.h> //for printf
#include <stdlib.h> //for malloc,free
#include <string.h> //for strlen
/*
Sapmple for inverting sting in plain c
Compile and run:
>gcc test.c -o test.bin && ./test.bin
*/
char* testString = "INVERT ME";
char* invertString(char* s){
int len = strlen(s) - 1;
for(int i = 0; i <= len/2; i++){
printf("%c <==> %c \r\n",s[i], s[len - i]);
if (s[i] != s[len - i]){
s[i] = s[i] ^ s[len - i];
s[len - i] = s[i] ^ s[len - i];
s[i] = s[i] ^ s[len - i];
}
}
return s;
}
int main(int argc, char* argv []){
char *tmpString = malloc(256);
strcpy(tmpString,testString);
char* result = invertString(tmpString);
printf("SOURCE = %s \n",testString);
printf("RESULT = %s \n",tmpString);
free(tmpString);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment