gpt4 book ai didi

c - 随机字符数组排序

转载 作者:行者123 更新时间:2023-11-30 17:05:32 24 4
gpt4 key购买 nike

我在编译程序时遇到问题,并且不确定我的逻辑和/或语法是否正确。我想做的是创建一个大小(由用户定义)的字符串数组,每个字符串 20 个字符。

//Melissa P. 
//University of Massachusetts Dartmouth
//CIS362
//2-6-2016

#include <stdio.h>

#define MIN 0 //For the random number generator
#define MAX 51 //so it can properly choose randomly a letter from my given array in getRandomString function

int getNum(void);
char * getRandomString(void);

int main (void)
{
int input = 0;
int i = 0;
int j = 0;
char word[20]; //placeholder for the random string result


printf("How many strings do you want in the array? "); //questions the user for amount of 20 character strings
scanf("%d", &input);
const char *array[input]; //pointer array of size defined by user

for (i = 0; i < input; i++)
{
*word = getRandomString(); //gets the 20 character string from the function
array[i] = *word; //and puts it in each space of the array
}

for (i = 0; i < input; i++)
{
printf("%s\n", array[i]); //prints the array
}

}

int getNum() //function to get a random number between 0 and 51.
{
int num;
num = rand()% ((MAX + 1) - MIN) + MIN;
return num;
}

char * getRandomString(void)
{
char word[20]; //declares the array for the random word to go in.
int num = 0;
int i = 0;
char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'}; //array of all lower case and upper case letters

for (i = 0; i < 20; i++)
{
num = getNum(); //gets a random number seed
word[i] = letterArray[num]; //fills the word array with a random letter
}
word[i] = '\0';

return *word; //returns the string
}

我得到的错误是这样的:“赋值从指针生成整数而不进行强制转换”这发生在 main 和 getRandomString 方法中。谢谢!

最佳答案

您的代码有很多错误,我已纠正您的错误,请尝试:

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

#define MIN 0 //For the random number generator
#define MAX 51 //so it can properly choose randomly a letter from my given array in getRandomString function

int getNum(void);
char * getRandomString(void);

int main (void)
{
int input = 0;
int i = 0;
int j = 0;
char* word; //placeholder for the random string result


printf("How many strings do you want in the array? "); //questions the user for amount of 20 character strings
scanf("%d", &input);
char **array = malloc(input * sizeof(char*)); //pointer array of size defined by user

for (i = 0; i < input; i++)
{
array[i] = malloc(21);
}

for (i = 0; i < input; i++)
{
word = getRandomString(); //gets the 20 character string from the function
strcpy(array[i],word); //and puts it in each space of the array
free(word);
}

for (i = 0; i < input; i++)
{
printf("%s\n", array[i]); //prints the array
}

//free memory
for (i = 0; i < input; i++)
{
free(array[i]);
}

free(array);
}

int getNum() //function to get a random number between 0 and 51.
{
int num;
num = rand()% ((MAX + 1) - MIN) + MIN;
return num;
}

char * getRandomString(void)
{
char* word = malloc(21); //declares the array for the random word to go in.
int num = 0;
int i = 0;
char letterArray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'}; //array of all lower case and upper case letters

for (i = 0; i < 20; i++)
{
num = getNum(); //gets a random number seed
word[i] = letterArray[num]; //fills the word array with a random letter
}
word[i] = '\0';

return word; //returns the string
}

你必须研究更好的指针,你必须使用 string.h 作为 strcpy 函数,使用 stdlib.h 作为 malloc。

关于c - 随机字符数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com