0
How to get an input of random letters in c
I need an input of say 10 random letters, what function does that?
3 Answers
+ 2
Kristy Brown
Store inputs in character array:
char arr[10];
for (int i = 0; i < 10; i++) {
scanf("%s", &arr[i]);
}
for (int i = 0; i < 10; i++) {
printf("%c\n", arr[i]);
}
+ 1
G'day Kristy Brown did you see the code bit from Jay?
It adds a few other headers as well as the normal stdio.h, specifically time.h and stdlib.h
You can use rand() to get you a random number.
It follows a pattern according to the seed that it gets, that is why it seeding srand() with time(NULL) gives an almost random number.
You need #include <stdlib.h> to get rand() and srand()
You need #include <time.h> to get time()
Then, once we have the random number, we modulo 26 to give values 0 to 25
We then use that +'a' to give ASCii values 97 to 122, which are the letters 'a' to 'z'
Cool š
{edit: wow, I missed the second use of rand() giving the possibility of capital or lowercase. You rock Jay Matthews }
+ 1
Use arrays