How to randomize words in a char array?

This is a discussion on How to randomize words in a char array? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a char array, eg. char sentence[100] = "They would come on time if you told them"; How ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    8

    How to randomize words in a char array?

    Hi,
    I have a char array, eg.

    char sentence[100] = "They would come on time if you told them";

    How can I pick up individual words and display them in different order? Will this work if I have the same number of arrays as is the number of words?

    Thanks

  2. #2
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    I would create an array of pointers char *wordStrings[20].
    Assuming a sentence is limited to no more than 20 words.
    Parse the sentence assinging each word string to a pointer.
    Than write a function that randomly selects a wordString pointer,
    or however many you desire, and display it.

    hth,

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    8

    Yes, array of pointers

    Thanks for the reply.

    I think it is the best way. Unfortunately I have to specify the number of pointers in advance and if I happen to have a very large sentence, this will not work.

    I wish C++ would have a unitily that would define new arrays and pointers additionally, in run time. Some sort of self programming. This way it would create the required number of arrays. But it is either a realizable dream or absolute nonsense for which I am sorry because I am a linguist a do not understand properly what is under the lid of C++.

    Thanks

    Frank

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. char array of words
    By sujeet1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 08:27 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21