Skip to content

Instantly share code, notes, and snippets.

@byronyi
Forked from k1xme/gist:e63557112ca2253e3d60
Created August 24, 2014 15:03
Show Gist options
  • Save byronyi/2db28dcc20e7430b854b to your computer and use it in GitHub Desktop.
Save byronyi/2db28dcc20e7430b854b to your computer and use it in GitHub Desktop.
/*
* Space complexity: O(1); Time complexity: O(n).
*/
void reverseString(char* str){
if(str == NULL) return; // Validation checking.
if(*str == "\0") return; // Empty string.
char temp; // For swapping tail and head.
char* head=str, tail=str; // Pointers for the beginning and the end of the string.
while(*tail != "\0") tail++; // Find the end of the string.
tail--;
while(head<=tail){ // Swap the string.
temp = *head;
*head = *tail;
*tail = temp;
head++;
tail--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment