如何按字母顺序对字符串数组进行排序并防止c中的突变

伊斯梅尔

如何在c中按字母顺序对字符串数组进行排序?

这是我的尝试:

/**
 * sort data alphabetically
 * @param data[] array of strings
 * @param size array size
 * @return an alphabetically sorted array
*/
char *sortAlphabetically(char *data[], int size)
{
  char *sortedArray = malloc(size);
  char *temp;
  int index, nextIndex;

  // copy data to avoid mutation
  for (int i = 0; i < size; i++)
    snprintf(&sortedArray[i], size, "%s", data[i]);

  // check data is not empty data
  if (!sortedArray)
    return NULL;

  for (index = 0; index < size; index++)
  {
    for (nextIndex = index + 1; nextIndex < size; nextIndex++)
    {
      // return positive integer if string1 is greater than string2, negative if lesser and 0 if equal. strmp compares both string chars by ASCII values
      if (strcmp(sortedArray[index], sortedArray[nextIndex]) > 0)
      {
        temp = sortedArray[index];
        sortedArray[index] = sortedArray[nextIndex];
        sortedArray[nextIndex] = temp;
      }
    }
  }
  return sortedArray;
}

int main()
{
  int size = 4;
  char *names[size];
  char *result;

  for (int i = 0; i < size; i++)
  {
    printf("(%d) Enter a name: ", i);
    scanf("%s", names[i]);
  }

  result = sortAlphabetically(names, size);

  if (result)
  {
    for (int i = 0; i < size; i++)
      printf("%s", result[i]);

    free(result);
    return 0;
  }
}

但是,我复制datasortedArrayusing的方式似乎出现了问题snprintf

我打算从sort函数返回一个新数组。

我到底在哪里弄错了,防止突变的最佳方法是什么?

chqrlie

sortedArray应该定义为,char **并分配为:

char **sortedArray = malloc(size * sizeof(*sortedArray));

还有其他问题:

  • 您不为从用户读取的字符串分配空间。您应该将字符串读入数组,并用于strdup()分配字符串的副本。
  • scanf("%s",...)这是一个安全漏洞:输入时间过长将导致scanf()写入操作超出目标数组的末尾。传递最大字节数以存储为%之间的数字s

这是修改后的版本:

/**
 * clone an array of strings
 * @param array (source)
 * @param array_size (source)
 * @return array
*/
char **cloneArray(char *source[], int size) {
    char **destination = NULL;
    if (source) {
        destination = malloc(size * sizeof(*destination));
        if (destination) {
            for (int i = 0; i < size; i++) {
                destination[i] = strdup(source[i]);
            }
        }
    }
    return destination;
}

/**
 * free an array of strings
 * @param array
 * @param array_size
 * @return void 
*/
void freeArray(char *array[], int size) {
    if (array) {
        for (int i = 0; i < size; i++) {
            free(array[i]);
        }
        free(array);
    }
}

/**
 * sort data lexicographically into a new array with duplicate strings
 * @param array array of strings
 * @param array_size array size
 * @return a lexicographically sorted array
*/
char **sortAlphabetically(char *data[], int size) {
    char **sortedArray = cloneArray(data, size);

    // check for allocation failure
    if (!sortedArray)
        return NULL;

    for (int index = 0; index < size; index++) {
        for (int nextIndex = index + 1; nextIndex < size; nextIndex++) {
            // return positive integer if string1 is greater than string2,
            //        negative if lesser and
            //        0 if equal.
            // strcmp compares both string chars by unsigned char values
            if (strcmp(sortedArray[index], sortedArray[nextIndex]) > 0) {
                char *temp = sortedArray[index];
                sortedArray[index] = sortedArray[nextIndex];
                sortedArray[nextIndex] = temp;
            }
        }
    }
    return sortedArray;
}

int main() {
    int size = 4;

    char **names = malloc(size * sizeof(*names));
    if (names == NULL)
        return 1;

    for (int i = 0; i < size; i++) {
        char buf[100];

        printf("\n(%d) Enter a name: ", i);

        if (scanf("%99s", buf) != 1)
            return 1;

        names[i] = strdup(buf);
        if (names[i] == NULL)
            return 1;
    }

    char **result = sortAlphabetically(names, size);

    if (result) {
        for (int i = 0; i < size; i++)
            printf("%s\n", result[i]);
        freeArray(result, size);
    }

    freeArray(names, size);
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

按字母顺序对C字符串数组进行排序

如何在Java中按字母顺序对字符串数组进行排序?

通过在c中按字母顺序对2d字符串数组进行排序

如何在C ++中按字母顺序对标准输入字符串进行排序?

在字符串中按字母顺序对字符进行排序

如何在Python中按字母顺序对字符串中的字母进行排序

如何按字母顺序对字符串的ArrayList进行排序?

如何按字母顺序对字符串进行排序

Python:如何按字母顺序对字符串中的字母进行排序,以区分大写和小写

如何根据名称和查询字符串按字母顺序对对象数组进行排序?

如何按字母顺序对记录名称为:字符串字段的记录数组进行排序?

如何使用角度orderBy过滤器按字母顺序对字符串数组进行排序?

如何使用基于域的列表按字母顺序对字符串数组进行排序

按字母顺序对字符串数组进行排序 C++

按字母顺序对字符串数组进行排序 - C++ 11

如何在Kotlin中按字母顺序对字符串进行排序

如何在R中按字母顺序对名称字符串进行排序?

在C中按字母顺序对字符串和结构进行排序

按字母顺序,数字顺序和特殊字符对字符串数组进行排序

如何按字母顺序对由逗号分隔的字符串进行排序,并按成员的姓氏按字母顺序进行排序?

在PostgreSQL中按字母顺序对字符串中的字母进行排序

在C ++中按字典顺序对字符串进行排序

按字母顺序对字符串变量中的字母进行排序

C#按字母顺序对字符串数组进行排序,注意将以大写字母开头的字符串放在首位。第一

如何使用sql按字符串排序的字母顺序对单词进行排序

Visual Basic字符串数组按字母顺序排序

使用指针按字母顺序对字符串数组进行排序

使用指针数组按字母顺序对字符串进行排序

Ruby:按字母顺序对字符串数组进行排序,其中包括一些字符串数组