如何计算句子输入 [英] How to count a sentence input

查看:55
本文介绍了如何计算句子输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的代码的这一小部分给了我麻烦。我应该输入一个句子,计算所述句子中的字母数,然后再显示一些信息。



我在输入,字母计数器或​​其他完全我不知道的事情上遇到了麻烦。



我的代码可能更容易解释问题所在。



 cout<<  输入您要打印的句子:<< ENDL; 

cin>>短语;

letLeng = phrase.size(); // 我假设问题就在这里。
totLet = PPL * letLeng;





那么我应该如何计算一句话中的字母,比如钱在哪里,以及我的其余部分代码可以做它需要做的事情吗?

解决方案

你可以选择两种方式,stdio和C ++标准库:



  #include      stdafx.h 
#include < stdio.h > ;

void _tmain( int argc ,_TCHAR * argv [])
{
const int datalength = 80 ;
char data [datalength];
printf( 输入内容:);
gets_s(data,datalength - 1 );
// ...依此类推
}





请参阅 http:// en。 cppreference.com/w/c/io/gets [ ^ ]。



使用标准C ++ iostream库:

  #include     stdafx.h  
#include < ; iostream >
#include < string >

使用 命名空间标准;

void _tmain( int argc,_TCHAR * argv [])
{
字符串数据;
cout<< 输入内容:; // 输出到标准输出流
cin>>数据; // 从std输入流到字符串对象的输入
// ......依此类推
}

请参阅:

http://www.cplusplus.com/reference/iostream/cout [ ^ ],

http://www.cplusplus.com/reference/iostream/cin [ ^ ]。



如果需要,您可以结合不同的方式。 />


-SA


我差点忘了回答有关计算信件的问题。



用字母来表示你的意思只是来自某些字母的字母...或所有语言的所有字母。

Ju st循环遍历所有字符,如果字符是字母则添加到计数器。如何确定这是否是信?



这是功能: http://www.cplusplus .com / reference / cctype / isalpha [ ^ ]。



如果您使用Unicode并且需要检查这是否是任何世界语言的字母,这一点很重要。如果你只需要计算一种语言的字母,你可以简单地检查一定代码点范围内的字符。



-SA

So this small section of my code is giving me trouble. I'm supposed to Input a sentence, Count the number of letters in said sentence, then display some information later.

I'm having trouble with either the input, the letter counter, or something else entirely that I'm not aware of.

It's probably easier for my code to explain where the problem lies.

cout << "Input the sentence you would like printed: " << endl;

cin >> phrase;

letLeng = phrase.size();  //I'm assuming the problem is in here.
totLet = PPL * letLeng;



So how should I go about counting the letters in a sentence like "Where's the money" so the rest of my code can do what it needs to do?

解决方案

You can choose between two ways, stdio and C++ standard library:

#include "stdafx.h"
#include <stdio.h>

void _tmain(int argc, _TCHAR* argv[])
{
    const int datalength = 80;
    char data[datalength];
    printf("Enter something: ");
    gets_s(data, datalength - 1);
    //... and so on
}



Please see http://en.cppreference.com/w/c/io/gets[^].

With standard C++ iostream library:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void _tmain(int argc, _TCHAR* argv[])
{
    string data;
    cout << "Enter something: "; // output to std output stream
    cin >> data; // input from std input stream to string object
    //... and so on
}

Please see:
http://www.cplusplus.com/reference/iostream/cout[^],
http://www.cplusplus.com/reference/iostream/cin[^].

You can combine different ways if you want.

—SA


I almost forgot to answer about counting letters.

A guess by "letters" you mean just letters from some alphabet… or all alphabet of all languages.
Just loop through all characters and add to a counter if a character is letter. How to determine if this is letter?

This is the function: http://www.cplusplus.com/reference/cctype/isalpha[^].

This is important if you use Unicode and need to check up if this is the letter of any of the world languages. If you need to count only the letters of just one language, you can simply check the character it is in the certain range of code points.

—SA


这篇关于如何计算句子输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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