Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法按字母顺序对C中的字符串列表进行排序_C_String_Algorithm_Sorting - Fatal编程技术网

无法按字母顺序对C中的字符串列表进行排序

无法按字母顺序对C中的字符串列表进行排序,c,string,algorithm,sorting,C,String,Algorithm,Sorting,我编写了一个程序,从用户那里接受5个字符串,然后使用冒泡排序算法按字母顺序显示它们。但是,字符串的显示顺序与输入顺序相同。请告诉我我做错了什么 #include <stdio.h> #include <stdlib.h> #include <string.h> void sSwap(char *s1, char *s2); int main(){ char *sList[5],input[100],*p; int i,j; put

我编写了一个程序,从用户那里接受5个字符串,然后使用冒泡排序算法按字母顺序显示它们。但是,字符串的显示顺序与输入顺序相同。请告诉我我做错了什么

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

void sSwap(char *s1, char *s2);

int main(){
    char *sList[5],input[100],*p;
    int i,j;

    puts("Enter 5 strings");
    for(i=0;i<5;i++){
        gets(input);
        sList[i] = (char *)malloc(strlen(input)+1);
        strcpy(sList[i],input);
    }

    puts("");

    for(i=3;i>=0;i--){
        for(j=0;j<=i;j++){
            if(strcmp(sList[j],sList[j+1])>0)
                sSwap(sList[j],sList[j+1]);
        }
    }

    for(i=0;i<5;i++)
        puts(sList[i]);
    return 0;
}

void sSwap(char *s1, char *s2){
    char *temp;
    temp = s1;
    s1 = s2;
    s2 = temp;
}
#包括
#包括
#包括
无效sSwap(字符*s1,字符*s2);
int main(){
字符*sList[5],输入[100],*p;
int i,j;
输入(“输入5个字符串”);
对于(i=0;i=0;i--){
对于(j=0;j0)
sSwap(sList[j],sList[j+1]);
}
}

对于(i=0;i,正如您被告知的,您的交换函数将获取值并按值交换,这意味着当您离开函数时,更改将不会保存,旧值将返回。请尝试此操作

void sSwap(char **s1, char **s2);

int main(){
    char *sList[5],input[100],*p;
    int i,j;

    puts("Enter 5 strings");
    for(i=0;i<5;i++){
        gets(input);
        sList[i] = (char *)malloc(strlen(input)+1);
        strcpy(sList[i],input);
    }

    puts("");

    for(i=3;i>=0;i--){
        for(j=0;j<=i;j++){
            if(strcmp(sList[j],sList[j+1])>0)
                sSwap(&sList[j],&sList[j+1]);
        }
    }

    for(i=0;i<5;i++)
        puts(sList[i]);
    return 0;
}

void sSwap(char **s1, char **s2){
    char *temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}
void sSwap(字符**s1,字符**s2);
int main(){
字符*sList[5],输入[100],*p;
int i,j;
输入(“输入5个字符串”);
对于(i=0;i=0;i--){
对于(j=0;j0)
sSwap(&sList[j],&sList[j+1]);
}
}

对于(i=0;i,正如您被告知的,您的交换函数将获取值并按值交换,这意味着当您离开函数时,更改将不会保存,旧值将返回。请尝试此操作

void sSwap(char **s1, char **s2);

int main(){
    char *sList[5],input[100],*p;
    int i,j;

    puts("Enter 5 strings");
    for(i=0;i<5;i++){
        gets(input);
        sList[i] = (char *)malloc(strlen(input)+1);
        strcpy(sList[i],input);
    }

    puts("");

    for(i=3;i>=0;i--){
        for(j=0;j<=i;j++){
            if(strcmp(sList[j],sList[j+1])>0)
                sSwap(&sList[j],&sList[j+1]);
        }
    }

    for(i=0;i<5;i++)
        puts(sList[i]);
    return 0;
}

void sSwap(char **s1, char **s2){
    char *temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}
void sSwap(字符**s1,字符**s2);
int main(){
字符*sList[5],输入[100],*p;
int i,j;
输入(“输入5个字符串”);
对于(i=0;i=0;i--){
对于(j=0;j0)
sSwap(&sList[j],&sList[j+1]);
}
}

对于(i=0;i,qsort函数在stdlib.h中

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
以及比较函数

static int compare (const void * a, const void * b)
{
    return strcmp (*(const char **) a, *(const char **) b);
}
然后,在主要情况下,您应该使用,取代sSwap

qsort (array, n_array, sizeof (const char *), compare);

qsort函数位于stdlib.h中

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
以及比较函数

static int compare (const void * a, const void * b)
{
    return strcmp (*(const char **) a, *(const char **) b);
}
然后,在主要情况下,您应该使用,取代sSwap

qsort (array, n_array, sizeof (const char *), compare);

您的问题之一是,当您交换两个字符串时,您不会检查是否必须将其与该字符串的新邻居交换

所以我会用一个递归函数来解决这个问题。比如,如果你交换,你调用这个函数(比如说
voidsort(char**list,int index)
):

如果字符串相等或顺序正确,如:

    sort(sList, currentListIndex);
所以你会:

void sort(char **sList, int index) {
     if (sList[index+1]) {
         if (strcmp(sList[index],sList[index+1]) > 0){
             sSwap(sList, index); // you can swap direclty on the tab
             return sort(sList, 0);
         }
         return sort(sList, index+1);
     }
     return void;
}

sort(sList, 0);

我已经有一段时间没有使用C了,所以可能指针是错误的,但这是一个想法

您的问题之一是,当您交换两个字符串时,您没有检查是否必须将其与该字符串的新邻居交换

所以我会用一个递归函数来解决这个问题。比如,如果你交换,你调用这个函数(比如说
voidsort(char**list,int index)
):

如果字符串相等或顺序正确,如:

    sort(sList, currentListIndex);
所以你会:

void sort(char **sList, int index) {
     if (sList[index+1]) {
         if (strcmp(sList[index],sList[index+1]) > 0){
             sSwap(sList, index); // you can swap direclty on the tab
             return sort(sList, 0);
         }
         return sort(sList, index+1);
     }
     return void;
}

sort(sList, 0);


有一段时间我没有使用C,所以可能指针是错误的,但这是因为你的交换函数不正确。它什么都不做。不要使用
get
,这是不安全的,用
fgets
代替。你的sSwap函数是一个NOOP。它需要使用
char**
参数,而不仅仅是
char*
@sh94
get
不起作用我们对它能“得到”的东西有一个固定的限制意思是你只是要求缓冲区溢出。@sh94这里你的交换函数是不正确的。它什么也不做。不要使用
get
,这是不安全的,用
fgets
代替。你的sSwap函数是一个NOOP。它需要接受
char**
参数,而不仅仅是
char*
@sh94
获取的内容没有固定的限制它能“得到”什么,这意味着你只是要求缓冲区溢出。@sh94看这里不应该是
char**
s吗?@DennisMeng你现在CoreTworking很好,但是为什么之前的一个不工作,数组包含字符串地址,这就是传递的内容,那么为什么要使用双指针呢?@sh94因为char*是指向的指针[a array of]char[s](字符串),char**是指向[a array of]string[s]的指针。您需要通过引用传递字符串,以便能够在sSwap函数之外显示修改内容。这些修改不应该是
char**
s吗?@DennisMeng您现在的CoreCt工作正常,但为什么之前的一个不工作,数组包含字符串地址,而这就是传递的内容,那么为什么是双指针呢?@sh94 beca使用字符*是指向[字符数组](字符串)的指针,字符**是指向[字符串数组]的指针。您需要通过引用传递字符串,以便能够在sSwap函数之外显示修改。如果为true,则不能说明OP代码错误的原因。如果为true,则不能说明OP代码错误的原因。