C 字符串插入排序程序

C 字符串插入排序程序,c,arrays,string,sorting,C,Arrays,String,Sorting,我刚开始学习C,所以我不太清楚。给出的程序告诉我们编写一个插入排序程序,该程序将20个字符串按空格分隔,然后按字母顺序排序并按顺序打印出来。这让我非常困惑,因为C没有字符串数据类型(至少据我所知)。字符串不只是字符数组吗?以下是我得到的: #include <stdio.h> #include <string.h> #define MAX_STRINGS 20 void InsertionSort(char list[]); void main() { int

我刚开始学习C,所以我不太清楚。给出的程序告诉我们编写一个插入排序程序,该程序将20个字符串按空格分隔,然后按字母顺序排序并按顺序打印出来。这让我非常困惑,因为C没有字符串数据类型(至少据我所知)。字符串不只是字符数组吗?以下是我得到的:

#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 20

void InsertionSort(char list[]);

void main()
{
    int index;
    char strings[MAX_STRINGS];

    /* Get input */
    printf("Enter %s strings.\n", MAX_STRINGS);
    for (index = 0; index < MAX_STRINGS; index++)
    {
        char tempString[100];
        printf("Input string %d : ", index);
        scanf("%s", &tempString[0]);
        strings[index] = tempString;
    }

    InsertionSort(strings);

    printf("\nThe input set, in alphabetical order:\n");
    for (index = 0; index < MAX_STRINGS; index++)
    {
        printf("%s\n", strings[index]);
    }
}

void InsertionSort(char list[])
{
    int unsorted;
    int sorted;
    char unsortedItem;

    for(unsorted = 1; unsorted < MAX_STRINGS; unsorted++)
    {
        unsortedItem = list[unsorted];

        for (sorted = unsorted - 1; (sorted >= 0) && (list[sorted] >  unsortedItem); sorted--)
        {
            list[sorted + 1] = list[sorted];
        }
        list[sorted + 1] = unsortedItem;
    }
}
#包括
#包括
#定义最大字符串数20
void InsertionSort(字符列表[]);
void main()
{
整数指数;
字符字符串[最大字符串];
/*获取输入*/
printf(“输入%s字符串。\n”,最大字符串);
对于(索引=0;索引=0)和&(列表[已排序]>未排序);已排序--)
{
列表[已排序+1]=列表[已排序];
}
列表[已排序+1]=未排序;
}
}

我对C和C语法完全陌生,我发现它非常混乱。此程序工作不正常。它所做的是允许我输入20个字符串,但没有排序,也没有打印任何内容。你知道怎么解决吗?还有,有没有办法让我输入一个句子,每个字符串之间用空格隔开?例如,如果我键入“我正在学习如何用C语言编程,但现在我不喜欢它。”这将给我16个字符串。“我”、“我”、“正在学习”等。谢谢。

使用此函数按字母顺序对字符串排序:

int s_bubblesortA(int argc,char **argv)
{
    int i , j = 0;
    char *p_1 , *p_2 , *tmp;
    while( j < argc )
    {
        for( i = 0 ; i < argc - j - 1 ; i++ )
        {
            p_1 = argv[i] , p_2 = argv[i+1];
            while( *p_1 && *p_2 )
            {
                if( *p_1 < *p_2 )
                    break;
                else if( *p_1 > *p_2 || ( ! *(p_2 + 1) && ( *p_1 == *p_2 ) && *(p_1+1) ) )
                {
                    tmp = argv[i];
                    argv[i] = argv[i+1];
                    argv[i+1] = tmp;
                    break;
                }
                p_1++;
                p_2++;
            }
        }
        j++;
    }
    return 0;
}
int s_bubblesortA(int argc,char**argv)
{
int i,j=0;
字符*p_1,*p_2,*tmp;
而(j*p_2 | |(!*(p_2+1)和&(*p_1==*p_2)和&*(p_1+1)))
{
tmp=argv[i];
argv[i]=argv[i+1];
argv[i+1]=tmp;
打破
}
p_1++;
p_2++;
}
}
j++;
}
返回0;
}

注意:选中此链接,以便我对类似帖子的完整回答。

使用此函数按字母顺序对字符串排序:

int s_bubblesortA(int argc,char **argv)
{
    int i , j = 0;
    char *p_1 , *p_2 , *tmp;
    while( j < argc )
    {
        for( i = 0 ; i < argc - j - 1 ; i++ )
        {
            p_1 = argv[i] , p_2 = argv[i+1];
            while( *p_1 && *p_2 )
            {
                if( *p_1 < *p_2 )
                    break;
                else if( *p_1 > *p_2 || ( ! *(p_2 + 1) && ( *p_1 == *p_2 ) && *(p_1+1) ) )
                {
                    tmp = argv[i];
                    argv[i] = argv[i+1];
                    argv[i+1] = tmp;
                    break;
                }
                p_1++;
                p_2++;
            }
        }
        j++;
    }
    return 0;
}
int s_bubblesortA(int argc,char**argv)
{
int i,j=0;
字符*p_1,*p_2,*tmp;
而(j*p_2 | |(!*(p_2+1)和&(*p_1==*p_2)和&*(p_1+1)))
{
tmp=argv[i];
argv[i]=argv[i+1];
argv[i+1]=tmp;
打破
}
p_1++;
p_2++;
}
}
j++;
}
返回0;
}

注意:请查看此链接,以便获得我对类似帖子的完整答案。

原始代码存在一些问题:

1) 不能使用
=
复制字符串;用于此(仅使用
=
分配指针)

2) 字符串是字符数组;因此,字符串数组就是字符数组(因此您的
InsertionSort
签名是错误的)。请注意,C字符串以null结尾,这意味着值为0的字节表示字符串的结束(这非常重要,如果您忘记了所有其他内容,请记住这一点)

3)
%s
需要一个
字符*
;此行生成UB:
printf(“输入%s字符串。\n”,最大字符串)。您需要的是
%d
(请阅读
printf
格式说明符)

4) 不能使用普通算术运算符比较字符串;那些比较指针。你需要使用

5) 您对插入排序算法的实现是错误的

6) 标准允许使用几种版本的
main
声明,
charmain
不是其中之一。在这种情况下,只需使用
intmain

以下是您的代码的固定版本:

#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 20
#define MAX_STRING_LEN 200

void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN]);

int main()
{
    int index;
    char strings[MAX_STRINGS][MAX_STRING_LEN];

    /* Get input */
    printf("Enter %d strings.\n", MAX_STRINGS);
    for (index = 0; index < MAX_STRINGS; index++)
    {
        printf("Input string %d : ", index);
        scanf("%199s", strings[index]);     // limit the width so we don't go past the buffer
        strings[index][sizeof(strings[index]) - 1] = '\0';
    }

    InsertionSort(strings);

    printf("\nThe input set, in alphabetical order:\n");
    for (index = 0; index < MAX_STRINGS; index++)
    {
        printf("%s\n", strings[index]);
    }
}

void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN])
{
    for (int i = 1; i < MAX_STRINGS; i++)
    {
        int j = i;

        while (j > 0 && strcmp(list[j - 1], list[j]) > 0)
        {
            char tmp[MAX_STRING_LEN];
            strncpy(tmp, list[j - 1], sizeof(tmp) - 1);
            tmp[sizeof(tmp) - 1] = '\0';

            strncpy(list[j - 1], list[j], sizeof(list[j - 1]) - 1);
            list[j - 1][sizeof(list[j - 1]) - 1] = '\0';

            strncpy(list[j], tmp, sizeof(list[j]));
            list[j][sizeof(list[j]) - 1] = '\0';

            --j;
        }
    }
}
#包括
#包括
#定义最大字符串数20
#定义最大字符串长度200
void InsertionSort(字符列表[最大字符串][最大字符串]);
int main()
{
整数指数;
字符字符串[MAX_strings][MAX_STRING_LEN];
/*获取输入*/
printf(“输入%d个字符串。\n”,最大字符串);
对于(索引=0;索引0&&strcmp(list[j-1],list[j])>0)
{
字符tmp[最大字符串长度];
strncpy(tmp,列表[j-1],sizeof(tmp)-1);
tmp[sizeof(tmp)-1]='\0';
strncpy(list[j-1],list[j],sizeof(list[j-1])-1;
列表[j-1][sizeof(列表[j-1])-1]='\0';