问题
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:
Incompatible types in assignmentof "int" to "char"[20]"
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?
回答1:
There are several issues with your code:
- You do not need
strncmp
to compare the first character - all you need is a simple==
or!=
. - Using a
do
without awhile
is a syntax error; you do not need a nested loop to solve your problem. character
is used to limit the progress ofi
in the outer loop, and also to compare to the first character of a word inwordmas[i]
. This is very likely a mistake.- Assuming that
wordmas
is an array, assigning towordmas
in the loop header is wrong.
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'
}
}
来源:https://stackoverflow.com/questions/22127149/finding-words-with-the-same-first-character