Created
May 1, 2017 16:04
-
-
Save samueltcsantos/163b07d00f57ab912632c07819bbacdd to your computer and use it in GitHub Desktop.
Reverse the string content using a function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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