I want codes for randomly selects the word (meaningful or meaningless) from 26 letters. The word contain 6 letters. I have the codes for C program or objective C, or you will give any idea to me.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
srand(time(NULL));
for (int i = 0; i < 6; ++i)
{
putchar('a' + (rand() % 26));
}
putchar('\n');
return EXIT_SUCCESS;
}
Salt to taste.
Update0
It would seem the OP does not know how to compile the above. If saved into a file called so.c, compile it with make so CFLAGS=-std=c99 from the same folder.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Written by kaizer.se */
void randword(char *buf, size_t len)
{
char alphabet[] = "abcdefghijklmnopqrstuvxyz";
size_t i;
for (i = 0; i < len; i++) {
size_t idx = ((float)rand()/RAND_MAX) * (sizeof(alphabet) -1);
buf[i] = alphabet[idx];
}
buf[len] = '\0';
}
int main(void) {
char buf[1024];
srand(time(NULL));
randword(buf, 7);
printf("Word: %s\n", buf);
return 0;
}
Test:
$ ./rword
Word: xoilfvv
Related
The code seems to only count one of the occurances when using scanf() or a defined getChar() I have set up but will count all and display when the string is defined in code. How can I get the output to work correctly while having a user input.
I've tried a few different approaches at taking the user input as seen but still I don't seem to be getting the result desired.
//#define _CRT_SECURE_NO_WARNINGS
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
int main() {
char *s;
fgets(&s[0],98,stdin);
int letter_counts[26] = {0};
int length = strlen(s);
char c;
for (int i = 0; i < length; i++) {
c = toupper(s[i]);
if (c >= 'A' && c <= 'Z')
letter_counts[c - 'A']++;
}
for (int i = 0; i < 26; i++) {
if (letter_counts[i] > 0) {
printf("%c%d ", 'A' + i, letter_counts[i]);
}
}
return 0;
}
I've Now changed it to use the fgets() but I don't seem to be using that correctly either...
Your main problem is that you don't allocate any space to s. Also introduces a couple of constants instead of your magic values for size:
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define ALPHA_LEN 'Z' -'A' + 1
#define STR_LEN 98
int main() {
char s[STR_LEN+1];
if(!fgets(&s[0],STR_LEN+1, stdin)) {
printf("gets failed\n");
return 1;
}
int letter_counts[ALPHA_LEN] = {0};
size_t length = strlen(s);
for (size_t i = 0; i < length; i++) {
char c = toupper(s[i]);
if (isupper(c))
letter_counts[c - 'A']++;
}
for (size_t i = 0; i < sizeof letter_counts / sizeof *letter_counts; i++) {
if (letter_counts[i]) {
printf("%c%d ", 'A' + i, letter_counts[i]);
}
}
printf("\n");
}
In this code you "randomly" fill your array with letters and if it hits the word you are looking for returns the amount of trys to get it correct. What I want to implement is a way of checking if it is spelled correct from the first character going onwards. Any ideas?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main() {
char *search= "SOMETHING";
int clk = clock();
printf ("clk=%d\n",clk);
srand(clk);
int len = strlen(search);
char *tafel = (char *) malloc (len+1);
tafel[len] = 0;
int pos;
char letter;
long i=0;
char check;
while (strcmp (search,tafel) != 0 ) {
i++;
letter = rand() % 26 +'A';
pos = rand() % len;
//pos=(pos+1)%len;
tafel[pos] = letter;
if (i%10000000==0) {
printf("%4ld mio: %s\n", i / 1000000, tafel);
printf ("%d\n",strcmp (search,tafel));
}
}
printf("It takes %ld trys to find '%s'\n",i,tafel);
printf ("%d\n",strcmp (suchwort,tafel));
return 0;
}
I am trying to write a program that deletes the last letter after each new line. I don't know why it isn't working but in case I set the value of i in the strncpy function with any number it works.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int i;
char s[]="ordinateur";
char a[strlen(s)+1];
for (i=0;i<=strlen(s);i++)
{
strncpy(a,s,(10-i));
printf("%s\n",a);
}
return 0;
}
The problem is that strncpy does not NUL terminate the string when you only copy a part of the source string. So you need to handle that yourself.
One way is to add this
memset(a, 0, strlen(s)+1);
just before the strncpy
Another way is to add this
a[10-i] = '\0';
just after the strncpy
That said, please notice that you don't need strncpy inside the loop to delete the last letter. You can simply do:
int main()
{
int i;
char s[]="ordinateur";
for (i=strlen(s); i>0 ; i--)
{
printf("%s\n",s);
s[i-1] = '\0'; // Remove last char
}
return 0;
}
This compiles and works (your sample wouldn't compile on my IDE because strlen doesn't return a constant.
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <cstring>
int main()
{
int i;
char s[] = "ordinateur";
char a[11 + 1];
for (i = 0; i <= strlen(s); i++)
{
strncpy_s(a, s, (10 - i));
printf("%s\n", a);
}
return 0;
}
You are relying on VLA which AFAIK is discouraged. Instead you should use memory allocation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
size_t i;
char s[] = "ordinateur";
char *a = calloc(strlen(s) + 1);
for (i = 0; i<= strlen(s); i++) {
strncpy(a,s, 10 - i);
printf("%s\n",a);
}
free(a);
return 0;
}
I have a problem generating a random string in a function.
In the code below I have used ASCII characters from 65 to 90. I want to include 48 to 57, skipping 58 to 64.
Is there any way to do this?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
int main()
{
char s[30];
random_string(s, 6,65,90);
printf("\n%s\n", s);
return 0;
}
void random_string(char * string, unsigned length,int min,int max)
{
/* Seed number for rand() */
srand((unsigned int) time(0) + getpid());
/* ASCII characters 33 to 126 */
unsigned int num_chars = length - 1;
unsigned int i;
for (i = 0; i < num_chars; ++i)
{
string[i] = rand() % (max - min + 1) + min;
}
string[num_chars] = '\0';
}
How about this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* wanted = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
void random_string(char* target, unsigned len, const char* sample, unsigned slen) {
while(len) {
*target = sample[rand() % slen];
++target, --len;
}
*target = '\0';
}
int main() {
char rnd_str[21];
//srand(...)
random_string(rnd_str, 20, wanted, strlen(wanted));
printf("%s\n", rnd_str);
}
I think that simplest way to achieve that is to reroll rand() if you have got number in unwanted range. To specify desired range, you can use bitmask or bool array instead of min/max.
It will look something like this:
do { c = rand() % 256; } while (!desired[c]);
string[i] = c;
From the code below I am trying to get the result of the result var into a string var but no success so far.
What's wrong? Why I can't get the right result? If I print this directly it's ok...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
char *string = "stelios";
unsigned char s[MD5_DIGEST_LENGTH];
int main()
{
int i;
unsigned char result[MD5_DIGEST_LENGTH];
MD5(string, strlen(string), result);
// output
for(i = 0; i < MD5_DIGEST_LENGTH; i++){
sprintf(s,"%0x", result[i]);//
printf("%x",s[i]);
}
printf("\n%x",s);
return EXIT_SUCCESS;
}
Try this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
char *string = "stelios";
char s[MD5_DIGEST_LENGTH * 2 + 1] = "";
int main()
{
int i;
unsigned char result[MD5_DIGEST_LENGTH];
MD5(string, strlen(string), result);
// output
for(i = 0; i < MD5_DIGEST_LENGTH; i++){
char temp[3];
sprintf(temp, "%02x", result[i]);
strcat(s, temp);
}
printf("Final Hex String: %s",s);
return EXIT_SUCCESS;
}
Each time sprintf is called, it writes the formatted value to the beginning of s, overwriting whatever was written there in the previous call. You need to do something like sprintf(s + i*2, "%02x", result[i]); (and change the length of s to 2*MD5_DIGEST_LENGTH+1).
Here's what I've used:
char *digit="0123456789abcdef";
char hex[2*MD5_DIGEST_LENGTH+1],*h;
int i;
for (h=hex,i=0; i<N; i++)
{
*h++=digit[digest[i] >> 4];
*h++=digit[digest[i] & 0x0F];
}
*h='\0';