Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C writing a code in C with strcmp that create dictionary

yesmoli12

New Coder
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:
C:
#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;
}
 
It seem like you just want to reverse the strings ? so if you read: h1 h2 h3 .... you want: h3 h2 h1 ?? if so then here is the code:

C:
#include <stdio.h>

int main()
{
    int n = 10;
    char str[n][11];

    for (int x = 0; x < n; x++)
        scanf("%s", str[x]);

    puts("");

    for (int x = n - 1; x >= 0; x--)
        printf("%s ", str[x]);

    return 0;
}

and you need an array of [10][11] if you want each string to be max 10 chars because 10 chars + '\0'
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom