随机字符数组排序

时间:2016-02-07 00:15:04

标签: c arrays string character

我在编译程序时遇到问题,而且我不确定我的逻辑和/或语法是否正确。我想要做的是创建一个字符串数组(由用户定义),每个字符串包含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
}

我得到的错误是:&#34;赋值在没有强制转换的情况下从指针生成整数&#34;这发生在main和getRandomString方法中。 谢谢!

2 个答案:

答案 0 :(得分:0)

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

#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。

答案 1 :(得分:0)

我看到的问题:

改善getRandomString

的建议

getRandomString有几个问题。

char* getRandomString(void)
{
   char word[20];
   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'};

   /// Problem ///
   /// When you have an array of 20 characters for a string
   /// you can fill at most 19 characters. The 20-th character
   /// needs to be the null character.
   for (i = 0; i < 20; i++)
   {
      num = getNum();
      word[i] = letterArray[num];
   }

   /// Problem ///
   /// By the time you are here, i is 20, one more than the highest valid index.
   /// You need to terminate the for loop with the conditional 
   /// i < 19
   word[i] = '\0';

   /// Problem ///
   /// *word evaluates to the first character of the string.
   /// The type it evaluates to is char, which does not match the
   /// return type of the function. Changing to "return word;" will
   /// be syntactically correct but that will be a problem. You will
   /// be returning a pointer to a string which lives only as long
   /// the function lives. The pointer will be a dangling pointer in
   /// the calling function.
   return *word;
}

您可以将其更改为:

char* getRandomString(void)
{
   char word[20];
   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'};

   // Stop the loop when i is 19
   for (i = 0; i < 19; i++)
   {
      num = getNum();
      word[i] = letterArray[num];
   }
   word[i] = '\0';

   // Return a string that is a copy of word.
   // The pointer will be valid in the calling function. 
   return strdup(word);
}

以上版本的getRandomString返回指向动态分配内存的指针。在调用函数中,您将不得不释放内存。

改善main

的建议

中删除const
const char *array[input];

成功

char *array[input];

word删除main的定义。你不需要它。捕获getRandomString返回的值所需的全部内容是:

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

在函数结束之前添加以下代码块以释放内存。

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

添加必要的#include语句

string.h需要strdupstdlib.h需要rand。添加

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

位于文件顶部。