Мне была назначена задача создания программы для сортировки строки (без использования указателей, потому что нас пока не изучали). Но я застрял, и мне нужна небольшая помощь. Это то, что у меня есть до сих пор:Сортировка строк в C
#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’
Может кто-нибудь сказать мне, что я делаю неправильно? Спасибо.
На самом деле слово 'int' в ошибке может быть важным. Вы уверены, что у вас есть 'char a [100] [100]'? Может ли это быть 'int a [100] [100]'? –