Writing a code in C with strcmp that create dictionary

hey i hope that someone here can help my with my question. in my homework I was asked to write a code that receives from the user 10 strings in size 10 Into an array of strings that will act like a dictionary. for example if the user enter bbb , ccc , abc , acb. the code will print abc acb bbb ccc. in our last lesson we learned about strcmp so i thought to use it here. this is what i got i know its worng but i have no idea what else to do so i hope someone here can help me. the code:

#include <stdio.h>

#include <string.h> int main() {

 int i;
int j;
int index;
char a_word[10][10];
printf("Enter string:\n");
for (i = 0; i < 10; i++)
{
    scanf("%s[^\n]", a_word[i]);
}
i = 0;
j = 1;
while (index<10)
{
    if (strcmp(a_word[i], a_word[j]) == -1)
    {
        j++;
        index++;
    }
    else if (strcmp(a_word[j], a_word[i]) <= 0)
    {
        i++;
        index++;
    }
}
if (strcmp(a_word[i], a_word[j]) <= 0)
{
    printf("%s",&a_word[i]);
}
else if (strcmp(a_word[j], a_word[i]) <= 0)
{
    printf("%s",&a_word[j]);
}
return 0;

If I understand it correctly, you code will only give out two values from the array, as whatever the while-loop ended up giving as i and j.

But you got 10 words to put out, in a specific order. Soooo, you gotta create that order - meaning you have to sort the array according to the condition. And then you create the output.
Easiest approach would be to go and adapt Bubble sort - Wikipedia in case you don’t feel like inventing your own sorting algorithm.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.