斯特伦没有';即使与#include<;字符串.h>;在C中

斯特伦没有';即使与#include<;字符串.h>;在C中,c,strlen,string.h,C,Strlen,String.h,它不返回int或其他什么吗? 这是我的代码片段: int wordlength(char *x); int main() { char word; printf("Enter a word: \n"); scanf("%c \n", &word); printf("Word Length: %d", wordlength(word)); return 0; } int wordlength(char *x) { int length =

它不返回int或其他什么吗? 这是我的代码片段:

int wordlength(char *x);

int main()
{
    char word;
    printf("Enter a word: \n");
    scanf("%c \n", &word);
    printf("Word Length: %d", wordlength(word));
    return 0;
}

int wordlength(char *x)
{
    int length = strlen(x);
    return length;
}
更改此部分:

char word;
printf("Enter a word: \n");
scanf("%c \n", &word);
致:

您还应该知道,如果启用了警告(例如,
gcc-Wall…
),编译器会帮助您解决大多数问题
更新:对于句子(即包含空格的字符串),您需要使用:


函数
strlen
应用于具有终止零的字符串(字符数组)。将函数应用于指向单个字符的指针。所以程序有未定义的行为。

A
char
不是一个字符串……但是我读到strlen想要一个char作为参数。所以,wordlength需要一个x字符。当用户输入一个单词时,它将被视为一个字符。另外,我的教授告诉我们在使用strlen时要放上。@Evyione:no,
strlen
需要一个
char*
(实际上是
const char*
),而不仅仅是
char
。你可能想读一下。“我读到strlen想要一个char作为参数”--不,你没有读到。。。这没有意义。“所以,wordlength需要一个字符x”--不,它需要一个指向字符的指针。。。这就是你给它的,但重点是
word
只有1个字符长。你需要让它成为一个字符数组,足够大,可以容纳你想要读的任何单词。我试着放一个句子,但它不计算空白?没错,上面的代码接受一个“单词”,正如你的缓冲区名称所暗示的那样。对于一个句子,您需要使用
fgets
(参见上面的编辑)。
char word[256];       // you need a string here, not just a single character
printf("Enter a word: \n");
scanf("%255s", word); // to read a string with scanf you need %s, not %c.
                      // Note also that you don't need an & for a string,
                      // and note that %255s prevents buffer overflow if
                      // the input string is too long.
char sentence[256];
printf("Enter a sentence: \n");
fgets(sentence, sizeof(sentence), stdin);