简体   繁体   English

用户输入字符串的耦合算法

[英]Couple algorithms for user-input strings

First of all i am new to coding and stackoverflow if i have obvious mistakes that i miss please forgive me. 首先,如果我有明显的错误想念,我是编码和stackoverflow的新手,请原谅。

Hello, i am writing c++ code and i need to get sentence from user and do a couple things with it. 您好,我正在编写C ++代码,我需要从用户那里获得句子,并用它做几件事。

  1. How many letter does your string have? 你的琴弦有几个字母?
  2. How many words does your string have? 您的字符串有多少个单词?

I am getting string with this code : 我得到此代码的字符串:

string str;
cout << "Please enter your string."<<endl;
getline(cin,str);
cout<< "Your string is : "<<a<<endl;
cout<<"Length of your string is : "<<str.length();

I am getting the length of string with str.length but it counts the spaces too my question is, how i determine the "space"? 我正在获取带有str.length的字符串的长度,但是它也计算空格。我的问题是,如何确定“空格”?

Since i dont have enough of reputations to post a comment,i am writing down here: 由于我没有足够的声誉来发表评论,因此我在这里写下:

Though @KABoissonneault is perfectly correct,but i would suggest giving a look at strtok too. 尽管@KABoissonneault是完全正确的,但是我建议也看一下strtok。 strtok can tokenize your string based on any delimiter(space being your case) strtok可以基于任何定界符对字符串进行标记化(空格取决于您的情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM