Hello, could someone help me create a program that will take in separate strings from a user and then output it all on one line in random order? This is what i have so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compareStrings(const void *pStr1, const void *pStr2) {
return strcmp(*(char **)pStr1, *(char **)pStr2);

}
 
 int main() {           
char **strings;
int n, i;
n = readLines(&strings);
(strings, n, sizeof(char *), compareStrings);
 printf("Your Sentence is: ");

      for (i = 0; i < n; i++)

            printf("%s\t", strings[i]);

      // free memory

      for (i = 0; i < n; i++)

            free(strings[i]);

      free(strings);

      system("PAUSE");

      return 0;

}


int readword(char **pStr) {

      char buf[256];

      int len;

      printf("Input string: ");

      fgets(buf, sizeof(buf), stdin);

      len = strlen(buf);

      // remove new line character

      if (buf[len - 1] == '\n')

            buf[len - 1] = 0;

      *pStr = (char *)calloc(len, sizeof(char));

      strcpy(*pStr, buf);

      return len;

}

 

int readLines(char ***pStrings) {

      int n, i;

      printf("How many words: ");

      scanf("%d", &n);

      // skip newline

      getc(stdin);

      *pStrings = (char **)calloc(n, sizeof(char *));

      for (i = 0; i < n; i++)

            readword(&(*pStrings)[i]);  

      return n;

}

Word shuffling pseudocode:

For earch word in sentence:
  Random_word1 = Random word  from [1..WordCount]
  Random_word2 = Random word  from [1..WordCount]
  Switch_words(Random_word1, Random_word2)

I'm sorry, I'm still confused!?!

I'm sorry, I'm still confused!?!

Did you read the wiki link given in the previous post ?

Yes I did. I just don't know how I would convert/implement that into my program to output random numbers.

Yes I did. I just don't know how I would convert/implement that into my program to output random numbers.

You first have an array of ints. Size of this array will be the numbers of words that you want to display. You initialize all the elements of this array to 0. Suppose your data consists of 4 words that. You create an array of 4 ints all initialized to 0.
According to the algorithm, you now choose a random number between 1-4. Suppose you choose 3, you print the 3 word and you make arr[2]=1 ie this word has already been displayed should not be displayed again.
Now you choose a random number between 1-3. Suppose you choose 3, you will print the word corresponding to arr[3] (word corresponding to arr[2] is already printed, so you should not include arr[2] when you count upto 3)

I hope this make it clear

Not exactly, what I'm trying to do is create a program that based on the words that the user puts in, the program will output those same words in random order. The link you provided gave me a little insight. It has a swap/shuffle function example. The only problem, is that it is in Java..so I would need to convert it to C.

dude, don't try to mindlessly copy or convert code.

he didnt link the code, anyhow. he linked the method.

so you need understand the method. understand how to do it. not how to copy.

this is your problem.


.

Not exactly, what I'm trying to do is create a program that based on the words that the user puts in, the program will output those same words in random order. The link you provided gave me a little insight. It has a swap/shuffle function example. The only problem, is that it is in Java..so I would need to convert it to C.

Dude I have explained you the logic .... You will have to figure out the conversion on your own

Whoa....relax guys. Jeph..no need to be rude. I think you took my words out of context. Abhimanipal, I appreciate your help and insight..I even said that the link you sent me was useful. Jeph..I never said I need to copy from someone or asked "can you give me the code". So nobody is mindlessly copying anything. I even stated that "I need to convert it to C". So that means that "I" am trying to figure out the method to implement it myself. I know the link was a method and thanks again abhimanipal, I'm looking at the link as we speak. Jeph..I thought this was a friendly site to help others out and your calling me names like "mindless" and saying things such as "it is your problem"...wow.

um, don't be all thinskinned. this is still the internets, it's all just words and stuff.

so, for one thing, i didn't call you mindless. the act of copying code is mindless.

and when i said "this is your problem" sorry you mistook my intent. the accent is on "this", not on "your" ... as in "understanding the method" is "the problem that you need to solve" ... or, in other words, "here is your problem, now you need to solve it"

sorry if you felt snubbed. do i need to handle you wearing kid gloves? would it help if I added smiley faces? :)

now if you post a specific question (other than "how do i do this?) we can give you specific answers. is there something else you require?

:)

And I repeat, I never said anything about copying code!! I never asked "How do you do this?" I asked for help in creating a program to output words in random order. The two other posters including abhimanipal helped with providing useful info. You on the other hand didnt provide anything but smart and rude remarks. Im a newbie poster/programmer, that doesnt mean you can talk down to ppl. Grow up!! If your not providing positive or helpful info, dont say nothin at all.Just keep it moving. "..handle you wearing kids gloves"...? lol good one

okay, sorry man. you're right. i shouldn't have used the word "copy". you never said that.

let's start over, shall we? :)

so tell me what is your question at this point? where are you still stuck?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.