Connecting between two codes in C

hello i hope for some help with my code.
i need to write code that recive 10 string from the user and sort them in alphabetical order and if the string that the user enter is lower the 10 letters i need to complete the word he enter with **** for exmaple:
“enter string”
abcd , aaaaaa.
printf:
aaaaaa***
abcd*****
explanation:
i sort the strings in alphabetical order
in the first string it was only 6 Letters so i add 3 ***
in the second string it was only 4 Letters so i add 5**
i was able to write the code but i do it in two different codes and i dont know how to connect them
first : the code that sort in alphabetical order
#include <string.h>
#include <stdio.h>

int main(){

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

}

the second code that add *** if there is less than 9 word
#include <stdio.h>

int main()
{
int i = 0;
printf(“enter string \n”);
char word[10] = {};
scanf("%s", word);
while (i < 9)
{
if (word[i] == ‘\0’)
word[i] = ‘!’;
i++;
}
printf("%s", word);
return 0;
}

I really hope that someone here can help me to connect this two codes

The easiest way would be to take the code for the input and cope-paste it in front of the sorting-code.

Next level would be to turn the code into seperate functions and files, which you then import into a main file (just like you use #include) where you just call those functions with appropriate arguments and return the new values.

I tried to do what bout of what you said but it didnt work.
ones i added this line
if (word[i] == ‘\0’)
word[i] = ‘!’;
it was giving me an error i think becuase i try to change the value of 1d array but i announced 2d array, when i do the same line but like that
if (word[i][j] == ‘\0’)
word[i][j] = ‘!’;
it didnt show an error but it also didnt work like i want so can you show me how i can do it right?

Well ofcourse you have to adjust things, like lengths and such.
Though you have to be more specific on what the issue is.
Generally you need two nested loops for this one - one for the words and then for the letters.

But if you got the general concept down, adressing a 2d array shouldn’t be an issue.

i try to explain, when i add the second code ( add !!! if there is less than 9 letters) i get two errors that said:
Comparison between pointer and integer (‘char *’ and ‘char’)
Array type ‘char [10]’ is not assignable
there is the new code:
#include <string.h>
#include <stdio.h>

int main() {

int i, j;
char word[10][10], s[10];
printf("Enter string:\n");
for (i = 0; i < 10; i++) {
    scanf("%s", word[i]);
}
for (i = 0; i < 10; i++) {
    for (j = i + 1; j < 10; j++) {
        if (strcmp(word[i], word[j]) == 1) {
            strcpy(s, word[i]);
            strcpy(word[i], word[j]);
            strcpy(word[j], s);
        }
    }
}
while (i<9)
 {
        if (word[i] == '\0')
            word[i] = '!';
        i++;
    }
    i = 0;
    while (i < 10) 
    {
        printf("%s \n", word[i]);
        i++;
    }
    return 0;
}

}
the errors appers here:
if (word[i] == ‘\0’)
word[i] = ‘!’;
and i dont know how to fix it, i said before i think it becuase i try to change the value of 1d array but i announced 2d array but i have no idea how i can fix it .

You loop over the 2d array, extract the 1d array and loop over that.

for i in length(array):
  for j in length(array[i]):
    --- code to do something with array[i][j] ---

This way array[i] is a 1d array and array[i][j] is a character in that.
Adjust into C-code.

(post deleted by author)

(post deleted by author)

I am really appreciate your help but i really dont understand what you mean can you please show it to me in my code ?

I gave you some code? Not C-code, but the concept is the same.
If word = [[abc],[def]]
then word[0] = [abc]
and word[0][2] = "c"

You just need to keep in mind to use two different values for iterating through the array and the sub-arrays. Like i for one and j for the other.

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