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>
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 .