0

I'm trying to create a little program that asks the user to enter two phrases. After entering two phrases, I display the phrases back to the user. Then I put both phrases into one variable, which I then display back a third time using one variable.

Next, I am trying to attempt to pick a letter at random from the two phrases and display the letter back to the user.

I've been doing some research online to see how people pick letters at random, but I haven't really seen anything other than picking words at random, but didn't really see how this could be applied to my own program.

thanks in advance.

P.S I'm noticing that when displaying the two phrases together, the second one is on a newline. Can anyone explain why that happens and how to get rid of it too? thanks

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


int main()
{
    srand(time(NULL));
    char phrase1[100], phrase2[100];
    printf("Please enter your first phrase\t");
    fgets(phrase1, 100, stdin);
    printf("Please enter your second phrase\t");
    fgets(phrase2, 100, stdin);
    printf("The first phrase you entered was: %s", phrase1);
    printf("The second phrase you entered was: %s", phrase2);
    strcat(phrase1, phrase2);
    phrase1[strlen(phrase1)-1] = ' ';
    printf("Both phrases put together in one sentence is: %s", phrase1);
    return 0;

}
a2800276
  • 3,272
  • 22
  • 33
Collwyr
  • 53
  • 7

2 Answers2

3

First seed the RNG:

srandom(getpid() ^ time(NULL));

Then get a random number between 0 and strlen(phrase)-1:

int rnum = random() % strlen(phrase);

Then print the character in question:

printf("random char: %c\n",phrase1[rnum]);

The reason you're seeing a newline in the concatenated string is that fgets adds the newline generated when you press Enter to the string. You can remove that by trimming the string by one character:

fgets(phrase1, 100, stdin);
phrase1[strcspn(phrase1, "\n")] = '\0';
...
fgets(phrase2, 100, stdin);
phrase2[strcspn(phrase1, "\n")] = '\0';
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Note: `phrase1[strlen(phrase1)-1]` has undefined behavior in the rare situation when the first character is an embedded `'\0'`. It also incorrectly truncates long strings missing a `'\n'`. `phrase1[strcspn(phrase1, "\n")] = 0;` works without those problems. – chux - Reinstate Monica Sep 16 '15 at 20:34
  • @chux Nice find. Fixed. – dbush Sep 16 '15 at 20:58
  • @dbush thanks very much for the informative reply, it helped me a lot. I wasn't aware you could get a random number between 0 and what was in phrase 1, (i haven't had much experience in random/randomly generating) so it was nice to learn something new. thank you for the fix with the newline too, I wasn't aware of the function strcspn so it was nice to also learn about this function and how you can use it, So thank you. – Collwyr Sep 16 '15 at 21:31
0

First question: - Find a way of generating a random integer between 0 and a certain integer A. - Make A the number of characters in your combined string. Say the resulting random integer is B. - print out YourString[B].

I feel like you can probably piece it together from that. At this stage in your learning process, getting code answers might not be beneficial.

Second question: I see you are replacing the end of the first string with a space, trying to catch \n. Depending on your environment, there may be an \r preceding it as well.

Jake
  • 822
  • 5
  • 14
  • Ive just realised this is occuring at the end of the concatenated string. Making answers on mobile devices is frustrating because you cannot see what you are replying to. My apologies. – Jake Sep 16 '15 at 20:27