I was given a file with several recipes denoted at the beginning by 0. Each recipe is followed by it's ingredients denoted at the beginning by 1 as shown below :
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",
I'm trying to alphabetically sort the ingredients in a list, but I'm getting segmentation fault. I would greatly appreciate your help on this. Here is what I've done:
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;
}