Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_WORDS 1000
#define MAX_LEN 256
 
void shuffle(char * pword[], int words, int iterations)
{
  int n;
 
  if ( words <= 1) return;
 
  for (n=0; n<iterations;>  {
    int i1, i2;
    i1 = rand()%words;
    i2 = rand()%words;
    char * tmp = pword[i1];
    pword[i1] = pword[i2];
    pword[i2] = tmp;
  }
}
 
int main()
{
  int n;
  char buf[MAX_LEN];
  char * pword[MAX_WORDS];
  int words = 0;
 
  FILE * fp = fopen("words.txt", "r");
 
  if ( ! fp ) return -1;
 
  while (!feof(fp))
  {
    if (words == MAX_WORDS) break;
    if (! fgets(buf, MAX_LEN, fp) ) break;
    buf[strlen(buf)-1] = '\0'; // remove newline
    pword[words] = strdup(buf); // this dynamically allocates memory
    words++;
  }
  fclose(fp);
 
  shuffle(pword, words, 2*words);
 
  for (n=0; n<words;>    printf("%s\n", pword[n]);
 
  // cleanup
  for (n=0; n<words;>    free( pword[n]);
 
  return 0;
}

if i want to pick one word form this void to the hangman game, what should i write? and void in what parameters should I have?

[Edit]Code block added[/Edit]
Posted
Updated 1-Dec-12 5:34am
v4
Comments
lewax00 30-Nov-12 21:54pm    
What have you tried so far? We aren't going to do your homework for you, but we will help if you have a specific problem.
arkhandio 30-Nov-12 22:50pm    
please help me to do this problem
lewax00 1-Dec-12 0:16am    
Help you with what? All you've done is post your assignment. Start working on it, and if you get stuck, ask for help then. No one here is going to write it for you.

1 solution

This looks like homework.

It would be best if you did your own homework. It is given to you so that you will learn something, think about what have been taught. Read your text books and give it a try.

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don't forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.

Good luck and happy coding.
 
Share this answer
 
Comments
__TR__ 1-Dec-12 6:59am    
+5.
If he takes your advice we may have a good programmer in the making :)
arkhandio 1-Dec-12 10:15am    
sorry for the question is too long, I've tried to make the other parts, but I do not understand how to make a list of words through external files such as txt, list of words written in the file and will be loaded in compilier, but should be incorporated into the array first, that loadnya fast, continue word must at random, please help to do it in the language of c, is established in a void

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900