Мне дали файл с несколькими рецептами, обозначенными в начале как 0. За каждым рецептом следуют его ингредиенты, обозначенные в начале как 1, как показано ниже:
char *rawRecipes[]={
"0Broccoli Coleslaw",
"1olive oil",
"1white vinegar",
"1white sugar",
"1package chicken flavored ramen noodles",
"1broccoli",
"1carrots",
"1green onions",
"1sunflower seeds",
"0Creamy Broccoli Salad",
"1broccoli",
"1red onion",
Я пытаюсь отсортировать ингредиенты в алфавитном порядке в списке, но у меня ошибка сегментации. Я был бы очень признателен за вашу помощь в этом. Вот что я сделал:
int main(void)
{
int input;
printf("\n");
printf("\n");
printf("Enter a command by number\n");
printf("4. List All Ingredients in alphabetical order\n");
printf("Give input: ");
scanf("%d", &input);
if (input == 4) // List All Ingredients in alphabetical order
{
int i = 0,k;
char alphabet[1000] ;
while(strcmp(rawRecipes[i], "") !=0)
{
if(rawRecipes[i][0] == 1 && rawRecipes[i+1][0] == 1)
{
char temp;
for(k=0; k<250; k++)
{
alphabet[k] = rawRecipes[i];
alphabet[k + 1] = rawRecipes[i + 1];
}
if(strcmp(alphabet[i], alphabet[i + 1] > 0))
{
temp = alphabet[i];
strcpy(alphabet[i], alphabet[i + 1]);
strcpy(alphabet[i + 1], temp);
}
}
i++;
}
int m;
for(m=0; m < 250; m++)
{
printf("%d: %c", m, alphabet[m]);
}
}
return 0;
}