查找词同第一个字符
问题描述:
我做了一个数组,现在我想比较两个字符串的第一个符号,如果这是真的来打印字。但我有一个问题:
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:
不兼容的类型assignmentof诠释到字符[20]
Incompatible types in assignmentof "int" to "char"[20]"
下面是code:
for ( wordmas= 0; i < character; i++ )
{
do {
if (!strncmp(wordmas[i], character, 1)
}
puts (wordmas[i]);
}
也许你们能帮助我吗?
Maybe you guys could help me?
答
有您的code几个问题:
There are several issues with your code:
- 您不需要
STRNCMP
来比较的第一个字符 - 你需要的是一个简单的==
或!=
。 - 使用
不要
没有,而
是一个语法错误;你并不需要一个嵌套循环来解决问题。 -
字符
用于限制的进展我
在外环,同时也比较第一在wordmas字的字符[I]
。这很可能是一个错误。 - 假设
wordmas
是一个数组,在循环头分配给wordmas
是错误的。
- 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.
在code查找单词,在一个特定的字符开始应该是这样的:
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'
}
}