计算字符串中的元音(c语言) [英] counting vowels in string (c language)

查看:61
本文介绍了计算字符串中的元音(c语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家,


我应该编写一个程序来计算字符串中的元音数量。

必须包含struct counter_t(必须在我编写的代码中声明)和函数int CountVowels(IN text [],OUT Count)。 OUT参数必须是counter_t类型。我写了代码,但似乎没有用。有人可以帮我吗???


------------------------------ -------------------------------------------------- -----------------------------

#include< stdio.h>


typedef struct {

int a;

int e;

int i;

int o;

int u;

} counter_t;


int CountVowels(char text [],counter_t Count);


int main()

{

char text [100];

counter_t Count;

Count.a = 0;

Count.e = 0;

Count.i = 0;

Count.o = 0;

Count.u = 0;

printf(" Text \ n");

得到(文本);

CountVowels(text,Count);

printf(" a =%d \ n",Count.a);

printf(" e =%d \ n",Count.e);

printf(" i =%d \ n",Count.i);

printf(" o =%d \ n",Count.o) ;

printf(" u =%d \ n",Count.u);

fflush(stdin);

getchar( );

返回0;

}


int CountVowels(char text [],counter_t Count)

{

int i;

for(i = 0; text [i]!=''\''',i ++)

{

switch(text [i])

{

case''a'':Count.a ++;打破;

case''e'':Count.e ++;休息;

case''我'':Count.i ++;休息;

case''o'':Count.o ++;打破;

case''u'':Count.u ++;休息;

}

}

返回(计数);

}

-------------------------------------------------- -------------------------------------------------- -

Hey everyone,

I am supposed to write a program that counts the number of vowels in string of characters.
It must include "Struct counter_t (it must be declared as in the code i''ve written)" and function "int CountVowels (IN text[], OUT Count)". the OUT parameter must be of type counter_t. I''ve written the code but it doesn''t seem to work. Can someone help me please???

-------------------------------------------------------------------------------------------------------------
#include <stdio.h>

typedef struct {
int a;
int e;
int i;
int o;
int u;
}counter_t;

int CountVowels (char text[], counter_t Count);

int main ()
{
char text[100];
counter_t Count;
Count.a = 0;
Count.e = 0;
Count.i = 0;
Count.o = 0;
Count.u = 0;
printf ("Text\n");
gets(text);
CountVowels(text, Count);
printf("a = %d\n", Count.a);
printf("e = %d\n", Count.e);
printf("i = %d\n", Count.i);
printf("o = %d\n", Count.o);
printf("u = %d\n", Count.u);
fflush (stdin);
getchar ();
return 0;
}

int CountVowels (char text[], counter_t Count)
{
int i;
for (i = 0; text[i] != ''\0'', i++)
{
switch(text[i])
{
case ''a'': Count.a++; break;
case ''e'': Count.e++; break;
case ''i'': Count.i++; break;
case ''o'': Count.o++; break;
case ''u'': Count.u++; break;
}
}
return (Count);
}
------------------------------------------------------------------------------------------------------

推荐答案



有两件事我看错了。由于CountVowels返回一个Count对象,因此其返回类型必须是Count,而不是int。成功:
Hi,
There are two things that I see wrong offhand. Since CountVowels returns a Count object, its return type has to be Count, not int. Make it:
展开 | 选择 | Wrap | 行号


我已经改进了我的代码了。但它仍然存在一些问题。 (该程序不必计算大写元音)。


#include< stdio.h>


typedef struct {

int a;

int e;

int i;

int o;

int u;

} counter_t;


counter_t CountVowels(char text [],counter_t * Count);


int main()

{

char text [100];

counter_t Count;

Count .a = 0;

Count.e = 0;

Count.i = 0;

Count.o = 0;

Count.u = 0;

printf(" Text \ n");

fgets(text,sizeof text,stdin);

Count = CountVowels(text ,计数);

printf(" a =%d \ n",Count.a);

printf(" e =%d \ n" ,Count.e);

printf(" i =%d \ n",Count.i);

printf(" o =%d \ n",Count.o);

printf(" u =%d \ n",Count.u);

getchar();

返回0;

}


counter_t CountVowels(char text [],counter_t * Count)

{

int i;

for(i = 0; text [i]!=''\'''; i ++)

{

开关(text [i])

{

case''a'':Count-> a ++;打破;

case''e'':Count-> e ++;休息;

case''我'':Count-> i ++;休息;

case''o'':Count-> o ++;打破;

case''u'':Count-> u ++;休息;

}

}

返回(计数);

}
well i''ve improved my code a bit. but it still has some problems. (the program doesn''t have to count upper case vowels).

#include <stdio.h>

typedef struct {
int a;
int e;
int i;
int o;
int u;
}counter_t;

counter_t CountVowels (char text[], counter_t* Count);

int main ()
{
char text[100];
counter_t Count;
Count.a = 0;
Count.e = 0;
Count.i = 0;
Count.o = 0;
Count.u = 0;
printf ("Text\n");
fgets(text, sizeof text, stdin);
Count = CountVowels(text, Count);
printf("a = %d\n", Count.a);
printf("e = %d\n", Count.e);
printf("i = %d\n", Count.i);
printf("o = %d\n", Count.o);
printf("u = %d\n", Count.u);
getchar ();
return 0;
}

counter_t CountVowels (char text[], counter_t* Count)
{
int i;
for (i = 0; text[i] != ''\0''; i++)
{
switch(text[i])
{
case ''a'': Count->a++; break;
case ''e'': Count->e++; break;
case ''i'': Count->i++; break;
case ''o'': Count->o++; break;
case ''u'': Count->u++; break;
}
}
return (Count);
}


你遇到了什么问题?我们既不是心灵感应也不是C编译器(或者有点酷,只看代码和BOOM!它已经编译好了),所以如果你能发布出错的地方,我们可以帮助你。


另外,请用[code]标签包围您的代码。它使阅读更容易。
What problems are you encountering? We''re neither telepathic nor C compilers (either would be kinda cool, just look at code and BOOM! it''s compiled), so if you can post what''s going wrong, we can help you.

Also, please surround your code with [code] tags. It makes it much easier to read.


这篇关于计算字符串中的元音(c语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆