Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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