字数 [英] word count

查看:59
本文介绍了字数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在编写一个我坚持使用的程序。我已经得到了要展示的人物,但不知道该怎么做并得到元音和辅音

__________________________________________________ _________

这是我要写的:

编写一个程序,提示用户输入字符串。输出将是字符串中的字符总数,字符串中辅音的总数以及字符串中元音的总数。务必提示输入并标记输出。代码需要将字符串作为字符数组(C字符串)和字符串数据类型(第79页和第446页)处理 - 这意味着您将看到两次相同的输出,一次是C字符串,一次对于字符串数据类型。例如:


输入字符串:程序


您输入:程序


这有7个字符,2个元音和5个辅音(C-String)。


这个有7个字符,2个元音和5个辅音(字符串数据类型)。


一定要找到你的c-的长度字符串和字符串的长度分别使用特定于c-string和字符串变量的函数。

解决方案

查看字符串的每个字符。如果它是字母表中的字母,那么检查是否是元音。


我对程序进行了一些修改,但它没有识别字符串str。

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

#include< iostream>

#include< string>


#define VOWEL_CHARACTERS" AEIOUaeiou"

int main()

{

string str;

string :: size_type length;

int name = 0,

VowelsCount = 0;

//获取输入。

cout<< 输入字符串:";

cin>>名称;

cout<< 你输入了: <<名称<< endl;

length = str.length();

//获取字符串的长度。

nStringLength = strlen(name);

for(name = 0; name< nStringLength; name ++)

{

//检查它是否是元音字符。

if(strchr(VOWEL_CHARACTERS,szInput [name])!= NULL)

{

//增加元音字符数。

nVowelsCount ++;

}

}

//打印结果。

cout<< ; 这有 << static_cast< unsigned int> (长度)<< "字符," << nVowelsCount元音;

endl;

system(PAUSE);

}


< blockquote>


我对程序进行了一些修改,但它没有识别字符串str。

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

#include< iostream>

#include< string>


#define VOWEL_CHARACTERSAEIOUaeiou


int main()

{

string str;

string :: size_type length;

int name = 0,

VowelsCount = 0;

//获取输入。 />
cout<< 输入字符串:";

cin>>名称;

cout<< 你输入了: <<名称<< endl;

length = str.length();

//获取字符串的长度。

nStringLength = strlen(name);

for(name = 0; name< nStringLength; name ++)

{

//检查它是否是元音字符。

if(strchr(VOWEL_CHARACTERS,szInput [name])!= NULL)

{

//增加元音字符数。

nVowelsCount ++;

}

}

//打印结果。

cout<< ; 这有 << static_cast< unsigned int> (长度)<< "字符," << nVowelsCount元音;

endl;

system(PAUSE);

}



这条线意味着什么? " nStringLength = strlen(name);"


strlen是一个重新调整char *长度的函数! (据我所知)


除了你因为strchr的论据而犯了错误!

看看:

http://www.cplusplus.com/reference/clibrary/cstring/strchr.html


这个例子对你有帮助!


又是什么是szInput ???


Hello I am writing a program that I have gotten stuck on. I have gotten the characters to show but don''t know how to do and get the vowels and consonants
__________________________________________________ _________
Here is what I have to write:
Write a program that prompts the user to input a string. The output will be the total number of characters in the string, the total number of consonants in the string, and the total number of vowels in the string. Be sure to prompt for input and to label your output. The code needs to handle the string as a character array (C-string) and as a string data type (p. 79 and 446) - so this means that you will see the same output twice, once for a C-string and once for a string data type. For example:

Enter a string: Program

You entered: "Program"

This has 7 characters, 2 vowels and 5 consonants (C-String).

This has 7 characters, 2 vowels and 5 consonants (String Data type).

Be sure to find the length of your c-string and the length of your string separately, using functions specific to c-string and string variables.

解决方案

Look at each character of the string. If it is a letter of the alphabet then check to see if is a vowel or not.


I have made some modifications to the program but it isn''t recognizing the string str.
---------------------
#include <iostream>
#include <string>

#define VOWEL_CHARACTERS "AEIOUaeiou"

int main()
{
string str;
string::size_type length;
int name = 0,
VowelsCount = 0;
// Get the input.
cout << "Enter the string : ";
cin >> name;
cout << "You entered: " << name << endl;
length = str.length();
// Get the length of the string.
nStringLength = strlen(name);
for(name = 0; name < nStringLength; name++)
{
// Check whether it is a vowel character.
if(strchr(VOWEL_CHARACTERS, szInput[name]) != NULL)
{
// Increment the vowel characters count.
nVowelsCount++;
}
}
// Print the result.
cout << "This has " << static_cast<unsigned int> (length) << "characters," << nVowelsCount "vowels";
endl;
system ("PAUSE");
}


I have made some modifications to the program but it isn''t recognizing the string str.
---------------------
#include <iostream>
#include <string>

#define VOWEL_CHARACTERS "AEIOUaeiou"

int main()
{
string str;
string::size_type length;
int name = 0,
VowelsCount = 0;
// Get the input.
cout << "Enter the string : ";
cin >> name;
cout << "You entered: " << name << endl;
length = str.length();
// Get the length of the string.
nStringLength = strlen(name);
for(name = 0; name < nStringLength; name++)
{
// Check whether it is a vowel character.
if(strchr(VOWEL_CHARACTERS, szInput[name]) != NULL)
{
// Increment the vowel characters count.
nVowelsCount++;
}
}
// Print the result.
cout << "This has " << static_cast<unsigned int> (length) << "characters," << nVowelsCount "vowels";
endl;
system ("PAUSE");
}

WHat does this line means? " nStringLength = strlen(name);"

strlen is a function which retuens the length of char * !!! (as far as i know)

besides you made mistake in order of the arguments for strchr!
take a look to:
http://www.cplusplus.com/reference/clibrary/cstring/strchr.html

The example will help you!

and again what is szInput???


这篇关于字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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