22271

Question:
I've made an array and now I'm trying to compare first symbols of two strings and if it's true to print that word. But I got a problem:
<blockquote>Incompatible types in assignmentof "int" to "char"[20]"
</blockquote>Here is the code:
for ( wordmas= 0; i < character; i++ )
{
do {
if (!strncmp(wordmas[i], character, 1)
}
puts (wordmas[i]);
}
Maybe you guys could help me?
Answer1:There are several issues with your code:
<ul><li>You do not needstrncmp
to compare the first character - all you need is a simple ==
or !=
.</li>
<li>Using a do
without a while
is a syntax error; you do not need a nested loop to solve your problem.</li>
<li>character
is used to limit the progress of i
in the outer loop, and also to compare to the first character of a word in wordmas[i]
. This is very likely a mistake.</li>
<li>Assuming that wordmas
is an array, assigning to wordmas
in the loop header is wrong.</li>
</ul>The code to look for words that start in a specific character should look like this:
char wordmas[20][20];
... // read 20 words into wordmas
char ch = 'a'; // Look for all words that start in 'a'
// Go through the 20 words in an array
for (int i = 0 ; i != 20 ; i++) {
// Compare the first characters
if (wordmas[i][0] == ch) {
... // The word wordmas[i] starts in 'a'
}
}