-
-
Save byronyi/2db28dcc20e7430b854b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* 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