gpt4 book ai didi

c - C 中的字符串排序

转载 作者:行者123 更新时间:2023-11-30 15:43:21 25 4
gpt4 key购买 nike

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

#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’

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

最佳答案

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

scanf ("%s" ...

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

&a[i][0]

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

关于c - C 中的字符串排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com