繁体   English   中英

在 C 中,按字符串长度对字符串数组进行排序

[英]In C, sort array of strings by string length

所以我将字符串输入到数组mydata[10][81]

while ((ct<=10) && gets(mydata[ct]) != NULL && (mydata[ct++][0] != '\0'))

然后我使用 for 循环创建第二个指针数组

for (i=0;i<11;i++){
    ptstr[i] = mydata[i];
}

这就是我卡住的地方我知道我需要以某种方式使用strlen ,但我什至无法想象如何获取指针的长度,然后根据长度的第三个附加值将该指针重新分配一个新位置

希望这是有道理的,我对如何做或解释它很迷茫,我只是想使用数组位置按长度对字符串进行排序(而不是使用类似qsort东西)

我做了一些更多的工作,并想出了这个:知道为什么它不起作用吗?

void orderLength(char *ptstr[], int num){
int temp;
char *tempptr;
int lengthArray[10];
int length = num;
int step, i, j, u;
for (i=0; i<num;i++){
    lengthArray[i] = strlen(ptstr[i]);
}
for (step=0; step < length; step++){
    for(j = step+1; j < step; j++){
          if (lengthArray[j] < lengthArray[step]){
              temp = lengthArray[j];
              lengthArray[j] = lengthArray[step];
              lengthArray[step] =temp;
              tempptr=ptstr[j];
              ptstr[j]=ptstr[step];

              }
          }
    }
    for (u=0; u<num; u++){
        printf("%s \n", ptstr[u]);
        }    
} 

正如Deduplicator的评论中所建议的,您可以使用stdlib.h定义的qsort

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

#define ROWS 4
#define MAXLEN 20

int compare (const void * a, const void * b) {
    size_t fa = strlen((const char *)a);
    size_t fb = strlen((const char *)b);
    return (fa > fb) - (fa < fb);
}

int main(int argc, const char * argv[]) {
    char arr[ROWS][MAXLEN] = {
        "abcd",
        "ab",
        "abcdefgh",
        "abc"
    };
    qsort(arr, ROWS, MAXLEN, compare);
    return 0;
}

你可以在这里看到它的运行情况。

一个非常简单的版本可能看起来像这样。 这是一个冒泡排序,对于任何合理大小的数据来说都很慢,但看起来你只对 11 个元素进行排序,所以在这里无关紧要。

关键是比较两个数组位置的长度的“if”语句。 如果它们乱序,接下来的三行会交换它们。

char* temp;
int length = 11;
int step, i;
for(step = 0; step < length - 1; step++)
    for(i = 0; i < length - step - 1; i++)
    {
        if(strlen(ptstr[i]) > strlen(ptstr[i+1]))
        {
            temp = ptstr[i];
            ptstr[i] = ptstr[i + 1];
            ptstr[i + 1] = temp;
        }
    }

编辑:如果要按字符串的内容而不是长度进行排序,则将 if 语句更改为:

if(strcmp(ptstr[i], ptstr[i + 1]) > 0)

(注意:您最好尽可能使用 str n cmp)

为了避免在同一个字符串上多次调用 strlen(),您可以使用如下所示的结构链:

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

typedef struct  t_elem
{
    char        data[81];
    int         length;
    t_elem      *next;
};

int     main(int ac, char **av)
{   
    t_elem      *head;
    t_elem      *recent;
    t_elem      *current;

    while (/* string have to be sorted */)
    {
        if (head == NULL) {
            head = (t_elem *)malloc(sizeof(t_elem));
            head->data = //readTheFirstString();
            head->length = strlen(head->data);
            head->next = NULL;
        }
        else {
            recent = (t_elem *)malloc(sizeof(t_elem));
            recent->data = //readTheNextString();
            recent->length = strlen(recent->data);
            recent->next = NULL;

            if (recent->length < head->length) {
                recent->next = head;
                head = recent;
            }
            else {
                current = head;
                while (current->next && current->next->length < recent->length) {
                    current = current->next;
                }
                recent->next = current->next;
                current->next = recent;
            }
        }
    }

    // print the sorted chained list
    current = head;
    while (current->next) {
        printf("%s\n", current->data);
        current = current->next;
    }

    // free the list
    current = head;
    while (current->next) {
        recent = current;
        current = current->next;
        free(recent);
    }
    return (0);
}
//C program to sort string based on string length.
#include<stdio.h>
#include<string.h>
int main()
{
    char a[200][200],temp[20];
    int i,j,n;
    printf("Enter no. of strings to be input = ");scanf("%d",&n);
    printf("Enter %d strings:\n",n);
    for(i=0;i<n;i++)
    scanf("%s",&a[i]);
//Sorting string based on length
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if(strlen(a[j])>strlen(a[j+1]))
            {
                strcpy(temp,a[j]);
                strcpy(a[j],a[j+1]);
                strcpy(a[j+1],temp);
            }
        }
    }
    printf("After Sorting :\n");
    for(i=0;i<n;i++)
    printf("%s\n",a[i]);
    return 0;
}
//Made By Capricious Coder; Happy Coding :D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM