Skip to content

Instantly share code, notes, and snippets.

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