Created
October 22, 2010 16:01
-
-
Save xoebus/640808 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
#include <stdio.h> | |
#include <string.h> | |
char* strrev(char* start, char* end) | |
{ | |
char *i = start; | |
char *j = end; | |
while (i < j) | |
{ | |
*i ^= *j; | |
*j ^= *i; | |
*i ^= *j; | |
++i; | |
--j; | |
} | |
return start; | |
} | |
int main() | |
{ | |
char str[50] = "My Name Is"; | |
int length = strlen(str); | |
printf("Input: %s\n", str); | |
// Reverse the string | |
strrev(str, str+length-1); | |
printf("Reversed Input: %s\n", str); | |
char *start_of_word = str; | |
// Reverse each word | |
int k; | |
for (k = 0; k < length; k++) | |
{ | |
if (str[k] == ' ' || str[k] == '\0') | |
{ | |
strrev(start_of_word, str + k - 1); | |
start_of_word = str + k + 1; | |
} | |
} | |
printf("Output: %s\n", str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment