【发布时间】:2014-03-22 05:26:42
【问题描述】:
我的代码有一些问题。第一:当我尝试编译时,我得到error: too few arguments to function 'strcmp'。我已经查看了所有内容并进行了多项更改,但仍然无法使其正常工作。第二:当我的代码编译时(如果我删除了 strcmp 部分),它将无法正确完成计数功能。有人可以帮忙吗?谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count(char array[], int size);
int stringLen(char array[]);
int convert(char ch);
int value, n;
int main()
{
//char * str;
//char s;
char a[100];
char b[100];
char c[100];
int charCount = stringLen(a);
int lCount = count(a, charCount);
printf("Enter your string: \n");
scanf("%s \n", a);
printf("Enter your string: \n");
scanf("%s \n", b);
printf("Enter your string: \n");
scanf("%s \n", c);
printf("The count is %d, length is %d\n", lCount, charCount);
int i;
for(i = 0; i < charCount; i++)
{
char c = a[i];
printf("Char %s = %d \n", &c, value);
}
n = strcmp(char string1[], char string2[], char string3[]);
printf("The first string in the alphabet is: %d \n", n);
return 0;
}
int stringLen(char array[])
{
char count;
int index;
while(array[index] !=0)
{
count++;
index++;
}
return count;
}
int count(char array[], int size)
{
int count;
int i;
for(i = 0; i < size; i++)
{
if(array[i] == 'a')
{
count ++;
}
else if(array[i] == 'A')
{
count ++;
}
}
return count;
}
【问题讨论】:
-
strcmp接受两个参数,而不是三个。此外,您将参数错误地传递给函数。我知道这是一个 C++ 参考链接,但这应该对您有所帮助:en.cppreference.com/w/c/string/byte/strcmp -
那么将其作为循环执行会更好吗?让它看看 A/B 然后 A/C 然后 B/C? (如果这是有道理的)
-
@iwant2flyaway:那可能就是这样。或者你可以使用一个数组并对其进行排序。您还需要为
stringLen()提供和实现(或改用strlen())。 -
问题是我有三个数组将接受用户输入,我不确定使用 C 处理 strcmp() 的最佳方法,因为我仍在学习它。
-
建议从
"%s \n"中删除" \n"。
标签: c string input count strcmp