在字符串中显示辅音数量的程序 [英] program that displays number of consonant in a string

查看:77
本文介绍了在字符串中显示辅音数量的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是一名学生,我想知道是否有人可以帮助我制作一个程序,该程序接受最大长度为10个字符的字符串并显示字符串中的辅音数.

Hello,

I''m a student and I would like to know if someone can help me make a program that accepts a character string with maximum length of 10 characters and displays the number of consonants in the string.

推荐答案

我认为这是家庭作业-因此,我将为您提供主要概述.由您决定将其转换为代码.

1)编写一个返回true/false的函数,将其称为IsConsonant.它应该接受一个char,如果字符 not 是辅音,则返回0.请记住,辅音可以是大写或小写!在继续之前进行测试.

2)在您的主程序中,执行以下操作:

2a)准备一个辅音计数,并将其设置为零.

2b)使用您先前编写的IsConsonant函数循环遍历字符串中的所有字符.如果当前字符是辅音,则在您的辅音计数中添加一个

2c)打印找到的辅音数量.

全部完成!

[edit]在C中,0为假,非零为true.我无所适从,错过了NOT一词.糟糕! -OriginalGriff [/edit]
I assume this is homework - so I''ll give you the main overview. It''s up to you to convert it to code.

1) Write a function, that returns true/false, call it IsConsonant. It should accept a char, and return 0 if the character not is a consonant. Remember that consonants can be Upper or Lower Case! Test it, before proceeding.

2) In your main program, do the following:

2a) Prepare a consonant count, and set it to zero.

2b) Loop through all characters in the string, using the IsConsonant function you wrote earlier. If the current character is a consonant, add one to your consonant count

2c) Print the number of consonants you found.

All done!

[edit]In C, 0 is false, non-zero is true. I goofed and missed out the word NOT. Oops! - OriginalGriff[/edit]


我用一种愚蠢的方式解决它~~~~希望它能为您提供帮助~~~
Hi I use a stupid way to solve it ~~~~wish it can give you some help~~~
#include<stdio.h>
int main()
{
    char c[10];
    int count=0;
    printf("please input 10 characters: ");
    for(int i=0;i<10;i++)
    {
        scanf("%c",&c[i]);
    }
    for(int j=0;j<10;j++)
    {
        if(c[j]=='a'||c[j]=='e'||c[j]=='i'||c[j]=='o'||c[j]=='u'||  \
            c[j]=='A'||c[j]=='E'||c[j]=='I'||c[j]=='O'||c[j]=='U')
            count++;
    }
    printf("There are %d consonant in the string.",10-count);
}


当您输入时,请勿使用空格或输入键,否则,可能有问题


when you type in ,do not use space or enter key ,otherwise,there maybe something wrong with it


所以~~~~我没有考虑您说的内容,请尝试一下:
so~~~~I didn''t consider what you said,now try this:
#include<stdio.h>
int main()
{
    char c[10];
    int count1=0;
    int count2=0;
    printf("please input 10 characters: ");
    for(int i=0;i<10;i++)
    {
        scanf("%c",&c[i]);
    }
    for(int j=0;j<10;j++)
        {
            if(c[j]=='a'||c[j]=='e'||c[j]=='i'||c[j]=='o'||c[j]=='u'||  \
            c[j]=='A'||c[j]=='E'||c[j]=='I'||c[j]=='O'||c[j]=='U')
            count1++;
    }
for(int m=0;m<10;m++)
    {
        if((c[m]>=65&&c[m]<=90)||(c[m]>=97&&c[m]<=122))
            count2++;
    }
    printf("There are %d consonant in the string.",count2-count1);
}


这篇关于在字符串中显示辅音数量的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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