Thread: Random Word Generator? Plz Help

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Random Word Generator? Plz Help

    OK, so I would much appreciate anybodys help with this problem.

    I have to come up with a program that will generate 1000 random words. These words don't have to be actual words in the dictionary, just a series of character. The words can be no longer than 9 characters. This is what I have so far....

    Code:
    #include <stdio.h>
    
    int main() {
    int i, j;
    char temp[9];
    
    for(i=0; i<1000; i++) {
    int run=rand()%7 + 3;
    for(j=0; j< run ; j++){
    temp[j]= rand()%26+'a';
    }
    temp[run] = '\0';
    printf("%s\n\n",temp);
    }
    
    
    system("PAUSE");
    return
    The first problem is that some of the words are more than 9 characters, and then the program only prints like 100 words not 1000. Any help or explanation would be great. Thanks in advanced.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try char temp[10]; You need an extra character for the trailing null when the length is 9 characters.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Question

    Thanks You, that fixed the number of character problems. It is still not printing out 1000 words though...Any ideas?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    
    int main(void)  
    {
    int i, j, run;
    char temp[9];
    
    for(i=0; i<1000; i++) 
      { run=rand()%7 + 3;
        for(j=0; j< run ; j++)
          { temp[j]= rand()%26+'a'; }
        temp[run] = '\0';
        printf("%d %s\n",i + 1,temp); }
    
    
    return 0;
    }
    Yes it does print a thousand words...

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Ok, that's strange. When I compile the code it only shows words 702-1000, on the actually screen...is there just something I am missing? Thx for your help

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jjwballer View Post
    Ok, that's strange. When I compile the code it only shows words 702-1000, on the actually screen...is there just something I am missing? Thx for your help
    Yeah... it's probably scrolling off the top of the screen.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe the other words scrolled off the screen too fast?

    Add:

    Code:
    if((i%100)==0)
      getchar();
    inside the (outer) for loop, to see

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Thanks guys, that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Word Count
    By simple in forum C Programming
    Replies: 12
    Last Post: 10-04-2002, 10:47 PM
  5. Word Count gone kookoo :)
    By Spectrum48k in forum C Programming
    Replies: 2
    Last Post: 05-17-2002, 09:12 PM