如何仅计算字符串中的字母?

问题描述 投票:0回答:1

我正在尝试编写一个计算字符串中多个元素的程序。其中第一个是字母。

该作业是CS50第2周问题集的一部分,因此包括库。

使用while条件,我能够对每个字符进行计数,但是一旦我添加了isalpha >>(它检查字符是否为字母数字),代码便停止工作。

我在做什么错?

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


int main(void)
{

    string text = get_string("Text: ");
    printf("%s, \n", text);

    int letters = 0;
    while (text[letters] !='\0')
    {
        if (isalnum(text[letters]))
        {
           letters++;
        }
    }
    printf("%i \n", letters);


}

我正在尝试编写一个计算字符串中多个元素的程序。其中第一个是字母。作业是CS50第2周问题集的一部分,因此包含了库。 ...

c loops c-strings cs50 isalpha
1个回答
0
投票

这里显示了如何定义正确的循环

© www.soinside.com 2019 - 2024. All rights reserved.