0

我的任务是编写一个对字符串进行排序的程序(不使用指针,因为我们还没有学过这些)。但我被困住了,我需要一点帮助。这是我到目前为止所拥有的:

#include <stdio.h>

int main()
{
    printf("Enter text to be sorted alphabetically:\n");
    char a[100][100]; // This is my array of text. 
    // It has a maximum of 100 words, each with 100 characters.
    int i = 0;
    while(scanf("%c", a[i][100]) != EOF)
    {
            i++; // This is where I get the string from the user.
            // I think this is where the problem is.
    }
    int l, x, j, m = 0;
    char k[100]; // This is the swap variable for the bubble sort.
    for(l = 0; l < i; l++)
    {
            for(j = 0; l < i - l; j++)
            {
                    if(a[j][m] > a[j+1][m]) 
                    { 
                            for(x = 0; x < 100; x++)
                            {
                                    k[m] = a[j][m]; // Bubble sort.
                                    a[j][m] = a[j+1][m];
                                    a[j+1][m] = k[m];
                            }
                            m = 0; // m is set back to 0.
                    }
                    if(a[j][m] == a[j+1][m])
                    {
                            m++; // m is supposed to represent the mth letter.
                            // so if the first two letters are equal, it increases m.
                            j--;
                    }
            }
    }
    printf("Sorted text is: /n");
    for(l = 0; l < i; l++)
    {
            for(j = 0; j < 100; j++)
            {
                    printf("%s", a[l][j]); // Print out the final result.
                    // I think I messed up this one too.
            }
            printf("\n");
    }
}

此代码拒绝编译。它说:

format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’

有人可以告诉我我做错了什么吗?谢谢。

4

1 回答 1

1

对于字符串,scanf 格式说明符是%s

scanf ("%s" ...

并且您必须提供存储字符串的地址

&a[i][0]

并且猎物没有人输入超过 99 个字符 :)

于 2013-11-09T18:20:02.647 回答