C - CS50 可读性无法编译

  • 本文关键字:编译 可读性 CS50 c cs50
  • 更新时间 :
  • 英文 :


谁能解释一下为什么它不编译?我忘了什么?我写条件语句的时候出了问题。大多数情况下,我有比较指针整数(char和char *)错误或比较字符串文字是未指定的错误()

编译错误

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{   
//Getting input from user
string s = get_string("Text: "); 
printf("%sn", 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 %in", letters);
printf("words %in", words);
printf("sentences %in", sentences);
}

当您使用s[i]时,您正在提取字符(类型char),这是字符串的一个元素,因此您必须将其与字符进行比较。

在C语言中必须使用单引号:

s[i] == 'a'