Skip to content

Instantly share code, notes, and snippets.

@xoebus
Created October 22, 2010 16:01
Show Gist options
  • Save xoebus/640808 to your computer and use it in GitHub Desktop.
Save xoebus/640808 to your computer and use it in GitHub Desktop.
#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