C语言输入一行字符,利用指针出其中英文字母

这个应该怎么修改??或者有什么更好的方案吗。一定要用指针

看题主原代码,是想输出字符串中的英文单词个数?

说实话,题主的原代码没看懂。。。

先给题主一份【统计单词个数】的代码吧

如果我理解有误,题主再追问~~

#include <stdio.h>
#include <conio.h>

#define LEN 255 /*数组长度上限*/

/*判断字符是否为字母*/ 
int chIsLetter (char *ch) {
    return ( (*ch>='a'&&*ch<='z') || (*ch>='A'&&*ch<='Z') );
}

/*统计字符串单词个数*/
int StrWordsCnt (char *str) {
    int wordsCnt=0;
    while (*str) {
        if (chIsLetter(str)&&!chIsLetter(str+1)) /*当前字符为字母且下一字符不是字母*/
            wordsCnt++; /*单词数+1*/
        str++;
    }
    return wordsCnt;
}

int main (void) {
    char str[LEN];
    //char str[LEN] = " Life is a journey, not a destination.";
    //char str[LEN] = "!HELLO WORLD.";    
    int wordsCnt=0;

    gets (str);
    //puts (str);
    wordsCnt = StrWordsCnt (str);
    printf ("%d\n", wordsCnt);
    putchar ('\n');

    getch (); /*屏幕暂留*/
    return 0;
}

运行结果

注:上图运行结果略去输入字符串部分

温馨提示:答案为网友推荐,仅供参考
相似回答