我需要编写一个需要 2 个单词并计算其长度的函数。我在下面写了一个,但这段代码只适用于第一个单词。我怎样才能改进它来计算整个句子?
#include <stdio.h>
int findlen(int *s);
int main(void)
{
char string1[80];
printf("Enter a string: ");
scanf("%s", string1);
printf("Lenght of %s is %d\n", string1, findlen(string1));
}
//find the length of the inputted string
int findlen(char *s)
{
int count = 0;
while (*s != '\0')
{
s++;
count++;
}
return count;
}
最佳答案
scanf
将仅接受一个单词输入..(即,当出现空格时它会中断..
尝试fgets
读取完整的字符串,直到\n
关于c - 以下功能无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992373/