Skip to content

Instantly share code, notes, and snippets.

@samueltcsantos
Created May 1, 2017 00:44
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 samueltcsantos/153bd6dad61d7d302baaa94cf65c9256 to your computer and use it in GitHub Desktop.
Save samueltcsantos/153bd6dad61d7d302baaa94cf65c9256 to your computer and use it in GitHub Desktop.
Reverse a text algoritm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* @desc Algorithm to reverse a string passed by command line as argument.
*
* Compile: * gcc -o invert-string.c -o invert
* Run: ./invert SAMARA
*
* @author Samuel T. C. Santos
*/
int main(int argc, char **argv){
char* text = argv[1];
//Get the text length
int len = strlen(text);
char tmp, i, j;
int middle = len / 2;
//First character
printf("first %c \n" , text[0]);
//Middle character
printf("middle %c \n" , text[middle]);
//last character
printf("last %c \n" , text[len-1]);
//Reverse the string
for(i=0, j=len-1; i < j; i++, j--){
tmp = text[i];
text[i] = text[j];
text[j] = tmp;
}
printf("Reverse: %s \n", text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment