#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void remove_random_word(char *str) {
char *words[128];
int word_count = 0;
char *token = strtok(str, " ");
// Split the string into words
while (token != NULL) {
words[word_count++] = token;
token = strtok(NULL, " ");
}
// Seed the random number generator
srand(time(NULL));
// Select a random word to remove
int random_index = rand() % word_count;
// Reconstruct the string without the random word
char tmpStr[1024] = "";
for (int i = 0; i < word_count; i++) {
if (i != random_index) {
strcat(strcat(tmpStr, words[i]), " ");
}
}
tmpStr[strlen(tmpStr) - 1] = '\0';
strcpy(str, tmpStr);
}
int main() {
char str[] = "Im not clumsy The floor just hates me";
printf("str: %s\n", str);
remove_random_word(str);
printf("result: %s\n", str);
return 0;
}
/*
run:
str: Im not clumsy The floor just hates me
result: Im not clumsy floor just hates me
*/