C 语言字符串中元音和辅音计数程序

如何编写一个C语言程序来计算字符串中的元音和辅音,并附带示例?

C 语言字符串中元音和辅音计数示例 1

此程序允许用户输入一个字符串(或字符数组)。然后,它将使用 If Else 语句计算用户指定字符串中存在的元音和辅音的数量。

/* C Program to Count Vowels and Consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	i = vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	while (str[i] != '\0')
  	{
  		if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
		str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
    	i++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
C Program to Count Vowels and Consonants in a String 1

首先,我们使用 While 循环 遍历字符串中的每个字符。

while (str[i] != '\0')
{

接下来,我们使用 If Else 语句 来检查每个字符是否等于 a、e、i、o、u、A、E、I、I、O。如果为 TRUE,则为元音,否则为辅音。

您可以使用 strlwrstrupr 将所有字符转换为相同的大小写。然后,在 C 编程 If 条件中检查小写或大写字母。

if(str[i] == 'a' || str[i]== 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
    str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')  {
	vowels++;
}

str[] = Hello World
i = 元音 = 辅音 = 0

While 循环第一次迭代: while (str[ i ] != 0)
条件为 True,因为 str[0] = H。

在 While 循环内部,我们使用 If Else 语句 来检查它是元音还是辅音。

if(str[i] == ‘a’ || str[i]== ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’ || str[i] == ‘A’ || str[i] == ‘E’ || str[i] == ‘I’ || str[i] == ‘O’ || str[i] == ‘U’)
=> if(H == ‘a’ || H== ‘e’ || H== ‘i’ || H== ‘o’ || H== ‘u’ || H == ‘A’ || H == ‘E’ || H == ‘I’ || H == ‘O’ || H == ‘U’)

上述条件为假。因此,它将执行 Else 块内的语句。
consonants++. 这意味着 consonants = 1

第二次迭代: while (str[ 1 ] != 0)
条件为 TRUE,因为 str[1] = e。
if(e == ‘a’ || e == ‘e’ || e == ‘i’ || e == ‘o’ || e == ‘u’ || e == ‘A’ || e == ‘E’ || e == ‘I’ || e == ‘O’ || e == ‘U’)
上述条件为 TRUE。因此,它将执行此块内的语句。
vowels++. 这意味着 vowels = 1

对于其余迭代执行相同操作

C 语言字符串中元音和辅音计数示例 2

程序 允许用户输入任何字符串值,并使用 ASCII 值 计算此字符串中的元音或辅音数量。

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if(str[i] == 97 || str[i] == 101 || str[i] == 105 || str[i] == 111 || str[i] == 117 ||
		str[i] == 65 || str[i] == 69 || str[i] == 73 || str[i] == 79 || str[i] == 85)  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  Tutorial Gateway

 Number of Vowels in this String = 7
 Number of Consonants in this String = 9

C 语言字符串中元音和辅音计数示例 3

此 C 语言元音和辅音计数程序与第一个示例相同,但这次我们使用了 函数 的概念来分离逻辑。

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>

int check_vowel(char);

int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if(check_vowel(str[i]))  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}

int check_vowel(char c)
{
    if (c >= 'A' && c <= 'Z')
       c = c + 32; 
 
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
       return 1;
 
    return 0;
}
 Please Enter any String :  hello everyone

 Number of Vowels in this String = 6
 Number of Consonants in this String = 8

C 语言字符串中元音和辅音计数示例 4

在此 程序 中,我们使用 Switch case(Else If 的替代方法)来计算元音和辅音。

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	int i, vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	for(i = 0; str[i] != '\0'; i++)
  	{
  		if( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') )  
		{
			switch(str[i])
			{
				case 'a':
				case 'e':
				case 'i':
				case 'o':
				case 'u':
				case 'A':
				case 'E':
				case 'I':	
				case 'O':
				case 'U':
					vowels++;
					break;
				default:
					consonants++;
    		}
    	}
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  learn c programming

 Number of Vowels in this String = 5
 Number of Consonants in this String = 12

C 语言字符串中元音和辅音计数示例 5

此用于计算元音和辅音的 程序 与第一个示例相同,但这次我们使用了 指针 的概念。

/* C Program to count vowels and consonants in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100];
  	char *s = str;
  	int vowels, consonants;
  	vowels = consonants = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	while (*s)
  	{
  		if(*s == 'a' || *s == 'e' || *s == 'i' || *s == 'o' || *s == 'u' ||
		*s == 'A' || *s == 'E' || *s == 'I' || *s == 'O' || *s == 'U')  
		{
  			vowels++;  	
 		}
  		else
    		consonants++;
    	s++;
	}
    printf("\n Number of Vowels in this String = %d", vowels);  
	printf("\n Number of Consonants in this String = %d", consonants);   	
  
  	return 0;
}
 Please Enter any String :  vowels and consonants

 Number of Vowels in this String = 6
 Number of Consonants in this String = 15

评论已关闭。