0

I want to pass a text file using input redirection to my program as a command line argument. I would then like the words in this to be randomly printed and whitespace removed.

I would like to run the program as

foo <dict.txt

dict.txt will contain various words, such as

camel penguin tiger chicken

I can currently print the contents of dict.txt using

    char line[100];

    while(scanf("%[^\n]%*c", &line) == 1) {
        printf("%s\n",line);

this would output

camel penguin tiger chicken

I would like it to be random and whitespace removed such as

penguinchickencameltiger

For the most part I am not sure how to approach it, I was thinking of putting it into an array and somehow randomizing it, or using regular expressions but I am not familiar with how to apply these.

Thanks in advance, I am a relatively new coder.

Jay
  • 73
  • 1
  • 2
  • 10
  • The `dict.txt` example you posted appears to be a single line. Is that the case, or is each word on a new line? Can you post a [mcve] that reproduces the problem? – ggorlen Mar 12 '19 at 01:17
  • 1
    Yes, you'd read the words into a static array or [a dynamic one](https://stackoverflow.com/questions/36801833/storing-each-line-of-a-text-file-into-an-array), then apply [Fisher-Yates shuffle](https://stackoverflow.com/questions/3343797/is-this-c-implementation-of-fisher-yates-shuffle-correct), then print the elements in the shuffled order without intervening spaces. – Antti Haapala -- Слава Україні Mar 12 '19 at 01:48
  • 1
    Note that if you use redirection `foo – Jonathan Leffler Mar 12 '19 at 05:45

0 Answers0