Created
August 25, 2013 02:51
-
-
Save dlion/6331691 to your computer and use it in GitHub Desktop.
Weekend Programming Challenge - issue #21 - Words Puzzle
http://olimex.wordpress.com/2013/08/23/weekend-programming-challenge-issue-21-words-puzzle/
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
/* | |
* Domenico Luciani | |
* domenicoleoneluciani@gmail.com | |
* Olimex Weekend Challenge #21 Words Puzzle | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
char* shuffle(char*); | |
int main(void) | |
{ | |
char old[300], | |
*new, *c; | |
printf("Insert text here: "); | |
fgets(old, 300, stdin); | |
// Simple hack to remove return character | |
if(c = strrchr( old, '\n' )) | |
*c='\0'; | |
//Split string in token and shuffle words | |
for(new = strtok(old," .,;:!-"); new != NULL; new = strtok(NULL, " .,;:!-")) | |
printf("%s ",shuffle(new)); | |
putchar('\n'); | |
return 0; | |
} | |
char* shuffle(char *word) | |
{ | |
char temp; | |
int len = strlen(word), | |
i, r; | |
if(len-2 >= 2) | |
{ | |
srand(time(NULL)); | |
for(i=1; i <= len-2; i++) | |
{ | |
r = (rand() % (len-1-i)) + i; | |
temp = word[r]; | |
word[r] = word[i]; | |
word[i] = temp; | |
} | |
} | |
return word; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment