在C中,按字符串长度对字符串数组进行排序 [英] In C, sort array of strings by string length

查看:241
本文介绍了在C中,按字符串长度对字符串数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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



对于(i = 0; i <11; i ++){
ptstr [i] = mydata [i]; pre>
}

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



希望这很有道理,我对如何执行或解释它迷失了,我只是尝试使用数组位置按长度对字符串进行排序(不使用像 qsort



我在上面做了更多工作,并想到了:
any

  void orderLength(char * ptstr [],int num){
int temp;
char * tempptr;
int lengthArray [10];
int length = num;
int step,i,j,u; (i = 0; i <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 printf(%s \n, ptstr [u]);
}
}


解决方案

As在 Deduplicator 的注释中建议,使用 qsort 在$中定义c $ c> stdlib.h 。

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

#define ROWS 4
#define MAXLEN 20

int比较(const void * a,const void * b){
size_t fa = strlen ((const char *)a);
size_t fb = strlen((const char *)b);
回报(fa&fb)-(fa< fb);
}

int main(int argc,const char * argv []){
char arr [ROWS] [MAXLEN] = {
watfi,
wh,
barified,
foo
};
qsort(arr,ROWS,MAXLEN,比较);
返回0;
}


So I input strings into an array mydata[10][81]

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

I then use a for loop to create a second array of pointers

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

This is where I get stuck I know I need to use strlen somehow, but I can't even conceive of how to get the length of a pointer and then re-assign that pointer a new position based on a third additional value of length

Hopefully that makes sense, I'm so lost on how to do it or explain it, I'm just trying to sort strings by length using array positions (not using something like qsort)

I did some more work on it and came up with this: any idea why its not working?

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]);
        }    
} 

解决方案

As suggested in the comments by Deduplicator, use qsort defined in stdlib.h.

#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] = {
        "watfi",
        "wh",
        "barified",
        "foo"
    };
    qsort(arr, ROWS, MAXLEN, compare);
    return 0;
}

这篇关于在C中,按字符串长度对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆