【发布时间】:2021-10-11 13:35:25
【问题描述】:
有人能解释一下为什么它不能编译吗? 我忘记了什么? 当我编写条件语句时出了点问题。 大多数时候我有比较指针整数(char和char *)错误或比较字符串文字未指定错误()
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
//Getting input from user
string s = get_string("Text: ");
printf("%s\n", s);
int letters = 0; // count letter
int words = 1; // count words, words +1 because 5 spaces = 6 words
int sentences = 0; // count sentences
for( int i = 0; i < strlen(s); i++)
{
if((s[i] >= "a" && s[i] <= "z") || (s[i] >= "A" && s[i] <= "Z"))
{
letters++;
}
if(s[i] == " ")
{
words++;
}
if(s[i] == "!" || s[i] == "?" || s[i] == ".")
{
sentences++;
}
}
printf("letters %i\n", letters);
printf("words %i\n", words);
printf("sentences %i\n", sentences);
}
【问题讨论】:
-
不要发布文本图片,而是将文本作为格式正确的文本发布。 (您的错误日志是可以复制/粘贴的文本)。
-
int words = 1;初始化几乎肯定是错误的,即使它后面有不透明的注释。您可以有前导或尾随空格,或者单词之间有多个空格。