C 程序 计算元音和辅音

来源:这里教程网 时间:2026-02-16 13:04:24 作者:

在本教程中,我们将编写一个 C 程序,使用指针计算给定字符串中的元音和辅音。

要了解这个程序,你应该知道数组指针在 C 中的基础知识。

使用指针计算字符串中的元音和辅音的程序

在下面的程序中,我们声明了一个char数组str来保存输入字符串,我们使用fgets()函数将其存储在数组中。我们已经将数组的基址(第一个元素的地址)赋给指针p。我们在while循环中使用指针p浏览输入字符串的所有字符,并在每次迭代时递增指针值。

#include <stdio.h>int main(){    char str[100];    char *p;    int  vCount=0,cCount=0;    printf("Enter any string: ");    fgets(str, 100, stdin);    //assign base address of char array to pointer    p=str;    //'\0' signifies end of the string    while(*p!='\0')    {        if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'                ||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')            vCount++;        else            cCount++;        //increase the pointer, to point next character        p++;    }    printf("Number of Vowels in String: %d\n",vCount);    printf("Number of Consonants in String: %d",cCount);    return 0;}

输出:

C 程序 使用指针计算字符串中的元音和辅音

相关 C 示例

    C 程序:使用指针打印字符串C 程序:用指针交换两个数字C 程序:创建初始化和访问指针变量

相关推荐