我刚开始学习C编程和练习,我发现一个任务。首先,我必须扫描两个字符串。然后,我必须逐字比较它们,如果有任何相同的字符,我必须打印出相同字符的数量。 它必须用指针来完成。因此,让我有“船”和“船”,所以程序将返回0.但如果它是“船”和“肥皂”它将返回2.c检查两个字符串为相同的字符
这是我迄今为止,但当我运行它会给我错误。我把错误置于评论中。
在此先感谢您的帮助。
#include <stdio.h>
#include <string.h>
int number_of_same_characters(char *, char *);
int main()
{
char * first[100];
char * second[100];
int result = 0;
printf("Enter first string\n");
gets(*first);
printf("Enter second string\n");
gets(*second);
result = number_of_same_characters(*first, *second);
printf("%d\n", result);
return 0;
}
int number_of_same_characters(char *p, char *q){ //i get this error here - error: invalid type argument of unary ‘*’ (have ‘int’)
int counter = 0;
for(int j = 0; *p[j] != '\0' || *q[j] != '\0'; ++j){ //i get this error here - error: invalid type argument of unary ‘*’ (have ‘int’)
if(strcmp(*p[j], *q[j])){
++counter;
}
}
return counter;
}
'字符*第一个[100];' - >'炭第一[100]; ' - >'获取(第一)' - >'number_of_same_characters(第一,第二)' - > ...等 – BLUEPIXY 2014-09-28 15:46:26
['gets' is evil](https://www.gnu.org/software/的libc /手动/ html_node /线路Input.html#指数得到)。不要使用它。 – 5gon12eder 2014-09-28 15:49:14