C 如何计算包含至少3个元音的单词数
我想知道我是否能寻求一些帮助。我正在用C编写一个程序,它可以写出字符串中的字符数、单词数和元音数(还添加了一些打印语句)。我试图找出如何编写一个代码,循环遍历字符串并计算包含至少3个元音的单词数。我觉得这似乎是一个非常容易编写的代码,但它总是最容易的事情,似乎我逃避。有什么帮助吗 另外:作为C语言的新手,我如何在使用函数C 如何计算包含至少3个元音的单词数,c,function,printing,count,C,Function,Printing,Count,我想知道我是否能寻求一些帮助。我正在用C编写一个程序,它可以写出字符串中的字符数、单词数和元音数(还添加了一些打印语句)。我试图找出如何编写一个代码,循环遍历字符串并计算包含至少3个元音的单词数。我觉得这似乎是一个非常容易编写的代码,但它总是最容易的事情,似乎我逃避。有什么帮助吗 另外:作为C语言的新手,我如何在使用函数int vouel\u count(char my\u sen[])而不是在我的main中使用代码时获得相同的结果 如果这有点让人困惑的话,我的意思是,既然我的main已经包含了
int vouel\u count(char my\u sen[])
而不是在我的main中使用代码时获得相同的结果
如果这有点让人困惑的话,我的意思是,既然我的main已经包含了计算输入中元音数量的代码,我怎么能将所说的代码转移到这个函数中,并且仍然在main中调用它呢
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SENTENCE 256
int main(void){
char my_sen[SENTENCE], *s; //String that containts at most 256 as well as a pointer
int words = 1, count = 0,vowel_word = 0; //Integer variables being defined
int i,vowel = 0, length; //More definitions
printf("Enter a sentence: ");//Input sentence
gets(my_sen);//Receives and processes input
length = strlen(my_sen); //Stores the length of the input within length
for(i=0;my_sen[i] != '\0'; i++){
if(my_sen[i]=='a' || my_sen[i]=='e' || my_sen[i]=='i' || my_sen[i]=='o' || my_sen[i]=='u' || //Loop that states if the input contains any of the following
my_sen[i]=='A' || my_sen[i]=='E' || my_sen[i]=='I' || my_sen[i]=='O' || my_sen[i]=='U') //characters(in this case, vowels), then it shall be
{ //stored to be later printed
vowel++;
}
if(my_sen[i]==' ' || my_sen[i]=='!' || my_sen[i]=='.' || my_sen[i]==',' || my_sen[i]==';' || //Similar to the vowel loop, but this time
my_sen[i]=='?') //if the following characters are scanned within the input
{ //then the length of the characters within the input is
length--; //subtracted
}
}
for(s = my_sen; *s != '\0'; s++){ //Loop that stores the number of words typed after
if(*s == ' '){ //each following space
count++;
}
}
printf("The sentence entered is %u characters long.\n", length); //Simply prints the number of characters within the input
printf("Number of words in the sentence: %d\n", count + 1); // Adding 1 to t[he count to keep track of the last word
printf("Average length of a word in the input: %d\n", length/count);//Prints the average length of words in the input
printf("Total Number of Vowels: %d\n", vowel);//Prints the number of vowels in the input
printf("Average number of vowels: %d\n", vowel/count);//Prints the average number of vowels within the input
printf("Number of words that contain at least 3 vowels: %d\n", vowel_word);//Prints number of words that contain at least 3 vowels
return 0;
}
#包括
#包括
#包括
#定义第256句
内部主(空){
char my_sen[句子],*s;//最多包含256个字符的字符串以及一个指针
int-words=1,count=0,元音\u-word=0;//正在定义整数变量
int i,元音=0,长度;//更多定义
printf(“输入句子:”;//输入句子
获取(my_sen);//接收并处理输入
length=strlen(my_sen);//在length内存储输入的长度
for(i=0;my_sen[i]!='\0';i++){
if(my|sen[i]=“a”| my|sen[i]=“e”| my|sen[i]=“i”| my|sen[i]=“o”| my|sen[i]=“u”|//循环说明输入是否包含以下任何内容
my_sen[i]='A'| my_sen[i]='E'| my_sen[i]='i'| my_sen[i]='O'| my_sen[i]==''U')//字符(在本例中为元音),则应为
{//存储以供以后打印
元音++;
}
if(my|sen[i]=''my|sen[i]=''!''my|sen[i]=''.''my|sen[i]='',''my|sen[i]=''类似于元音循环,但这一次
my_sen[i]='?')//如果在输入中扫描以下字符
{//则输入中字符的长度为
长度--;//减去
}
}
对于(s=my_sen;*s!='\0';s++){//循环,它存储在之后键入的字数
如果(*s==“”){//下面的每个空格
计数++;
}
}
printf(“输入的句子长度为%u个字符。\n”,长度);//只打印输入中的字符数
printf(“句子中的字数:%d\n”,count+1);//将1添加到t[计数以跟踪最后一个单词
printf(“输入中单词的平均长度:%d\n”,长度/计数);//打印输入中单词的平均长度
printf(“元音总数:%d\n”,元音);//打印输入中的元音数
printf(“平均元音数:%d\n”,元音/计数);//打印输入中的平均元音数
printf(“包含至少3个元音的单词数:%d\n”,元音\u单词);//打印包含至少3个元音的单词数
返回0;
}
1)获取字符串
2) 使用strtok()将每个单词按空格分隔
3) 逐字符循环检查每个字符串是否为元音。1)获取字符串
2) 使用strtok()将每个单词按空格分隔
3) 逐字符循环检查每个字符串是否为元音。请检查下面的代码
#include<stdio.h>
#include <string.h>
int count_vowels(char []);
int check_vowel(char);
main()
{
char array[100];
printf("Enter a string\n");
gets(array);
char seps[] = " ";
char* token;
int input[5];
int i = 0;
int c = 0;
int count = 0;
token = strtok (array, seps);
while (token != NULL)
{
c = 0;
c = count_vowels(token);
if (c >= 3) {
count++;
}
token = strtok (NULL, seps);
}
printf("Number of words that contain atleast 3 vowels : %d\n", count);
return 0;
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if ( flag == 1 )
count++;
c++;
}while( d != '\0' );
return count;
}
int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */
if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
#包括
#包括
int count_元音(char[]);
int-check_元音(char);
main()
{
字符数组[100];
printf(“输入字符串”);
获取(数组);
字符seps[]=“”;
字符*令牌;
int输入[5];
int i=0;
int c=0;
整数计数=0;
令牌=strtok(数组,seps);
while(令牌!=NULL)
{
c=0;
c=计数_元音(标记);
如果(c>=3){
计数++;
}
令牌=strtok(空,seps);
}
printf(“包含至少3个元音的单词数:%d\n”,计数);
返回0;
}
int count_元音(字符a[]
{
整数计数=0,c=0,标志;
chard;
做
{
d=a[c];
flag=检查元音(d);
如果(标志==1)
计数++;
C++;
}而(d!='\0');
返回计数;
}
int check_元音(字符a)
{
如果(a>='a'&&a请检查下面的代码
#include<stdio.h>
#include <string.h>
int count_vowels(char []);
int check_vowel(char);
main()
{
char array[100];
printf("Enter a string\n");
gets(array);
char seps[] = " ";
char* token;
int input[5];
int i = 0;
int c = 0;
int count = 0;
token = strtok (array, seps);
while (token != NULL)
{
c = 0;
c = count_vowels(token);
if (c >= 3) {
count++;
}
token = strtok (NULL, seps);
}
printf("Number of words that contain atleast 3 vowels : %d\n", count);
return 0;
}
int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;
do
{
d = a[c];
flag = check_vowel(d);
if ( flag == 1 )
count++;
c++;
}while( d != '\0' );
return count;
}
int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */
if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
return 0;
}
#包括
#包括
int count_元音(char[]);
int-check_元音(char);
main()
{
字符数组[100];
printf(“输入字符串”);
获取(数组);
字符seps[]=“”;
字符*令牌;
int输入[5];
int i=0;
int c=0;
整数计数=0;
令牌=strtok(数组,seps);
while(令牌!=NULL)
{
c=0;
c=计数_元音(标记);
如果(c>=3){
计数++;
}
令牌=strtok(空,seps);
}
printf(“包含至少3个元音的单词数:%d\n”,计数);
返回0;
}
int count_元音(字符a[]
{
整数计数=0,c=0,标志;
chard;
做
{
d=a[c];
flag=检查元音(d);
如果(标志==1)
计数++;
C++;
}而(d!='\0');
返回计数;
}
int check_元音(字符a)
{
如果(a>='a'&&a这不是什么大问题
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int vowel_count(char my_sen[])
{
int wcount = 0; // number of words with 3+ vowel chars
int vcount = 0; // current number of vowel chars in the current word
int i = 0; // index into the string
int ch;
while ((ch = my_sen[i++]) != '\0')
{
if (isspace(ch) || !isalpha(ch))
{
// ch is not an alphabetical char, which can happen either
// before a word or after a word.
// If it's after a word, the running vowel count can be >= 3
// and we need to count this word in.
wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
vcount = 0; // reset the running vowel counter
continue; // skip spaces and non-alphabetical chars
}
if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these
{
++vcount; // count vowels
}
}
// If my_sen[] ends with an alphabetical char,
// which belongs to the last word, we haven't yet
// had a chance to process its vcount. We only
// do that in the above code when seeing a non-
// alphabetical char following a word, but the
// loop body doesn't execute for the final ch='\0'.
wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
return wcount;
}
int main(void)
{
char sen[] = "CONSTITUTION: We the People of the United States...";
printf("# of words with 3+ vowels in \"%s\" is %d", sen, vowel_count(sen));
return 0;
}
顺便说一句,你可以修改这个函数来计算所有你需要的东西。它已经找到了单词的开始和结束位置,因此,简单的单词计算很容易实现。还有单词长度等等。这不是什么问题
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int vowel_count(char my_sen[])
{
int wcount = 0; // number of words with 3+ vowel chars
int vcount = 0; // current number of vowel chars in the current word
int i = 0; // index into the string
int ch;
while ((ch = my_sen[i++]) != '\0')
{
if (isspace(ch) || !isalpha(ch))
{
// ch is not an alphabetical char, which can happen either
// before a word or after a word.
// If it's after a word, the running vowel count can be >= 3
// and we need to count this word in.
wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
vcount = 0; // reset the running vowel counter
continue; // skip spaces and non-alphabetical chars
}
if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these
{
++vcount; // count vowels
}
}
// If my_sen[] ends with an alphabetical char,
// which belongs to the last word, we haven't yet
// had a chance to process its vcount. We only
// do that in the above code when seeing a non-
// alphabetical char following a word, but the
// loop body doesn't execute for the final ch='\0'.
wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
return wcount;
}
int main(void)
{
char sen[] = "CONSTITUTION: We the People of the United States...";
printf("# of words with 3+ vowels in \"%s\" is %d", sen, vowel_count(sen));
return 0;
}
顺便说一句,你可以改变这个函数来计算所有你需要的东西。它已经找到了单词的开始和结束位置