C++ c++;对数组中的字进行计数
我需要编写一个函数来获取一个字符串,并计算字符串中有多少单词和多少字母。然后计算它的平均值。 字符串中的单词是由一个或多个空格分隔的字母和数字序列 首先,我必须检查字符串是否正确。字符串只能包含小写字母、大写字母和数字 我没有正确计算所有种类的单词,而且我的函数也没有计算最后一个字母C++ c++;对数组中的字进行计数,c++,arrays,C++,Arrays,我需要编写一个函数来获取一个字符串,并计算字符串中有多少单词和多少字母。然后计算它的平均值。 字符串中的单词是由一个或多个空格分隔的字母和数字序列 首先,我必须检查字符串是否正确。字符串只能包含小写字母、大写字母和数字 我没有正确计算所有种类的单词,而且我的函数也没有计算最后一个字母 #include <iostream> using namespace std; #include <string.h> #define SIZE 50 float checkStrin
#include <iostream>
using namespace std;
#include <string.h>
#define SIZE 50
float checkString(char string[]) {
float wordCounter = 0;
float letterCounter = 0;
bool isLegit = true;
int i = 0;
while (isLegit) {
if (((string[i] >= 48 && string[i] <= 57) ||
(string[i] >= 65 && string[i] <= 90) ||
(string[i] >= 97 && string[i] <= 122 ))) {
for (int j = 0; j <= strlen(string); j++) {
if ((string[j - 1] != ' ' && string[j] == ' ' &&
string[i + 1] != ' ')
|| j == (strlen(string) - 1)) {
wordCounter++;
}
else if (string[j] != ' ') {
letterCounter++;
cout << string[j];
}
}
cout << " The avareage is : " << (letterCounter /
wordCounter) << endl;
isLegit = false;
}
else {
return -1;
isLegit = false;
}
}
cout << "Number of words " << wordCounter << endl;
cout << "Number of letters " <<letterCounter << endl;
}
int main() {
char string[SIZE];
cout << "please enter a sentence " << endl;
cin.getline(string, SIZE);
checkString(string);
}
#包括
使用名称空间std;
#包括
#定义尺寸50
浮点校验字符串(字符字符串[]){
浮点字计数器=0;
浮点数=0;
bool isLegit=true;
int i=0;
while(isLegit){
如果((string[i]>=48&&string[i]=65&&string[i]=97&&string[i]而不是使用char[]对于字符串,我建议使用 STD::String ,它可以动态地生长和收缩。它是标准C++库中最常用的类型之一。也可以使用<代码> String Strue,让您在其中放入一个字符串,然后可以使用<代码> > <代码>提取String String的内容,就像从std::cin
读取时一样
代码中带有注释的示例:
#include <iostream>
#include <sstream> // std::stringstream
#include <string> // std::string
// use std::string instead of a char[]
float checkString(const std::string& string) {
// put the string in a stringstream to extract word-by-word
std::istringstream is(string);
unsigned words = 0;
unsigned letters = 0;
std::string word;
// extract one word at a time from the stringstream:
while(is >> word) {
// erase invalid characters:
for(auto it = word.begin(); it != word.end();) {
// Don't use magic numbers. Put the character literals in the code so
// everyone can see what you mean
if((*it>='0' && *it<='9')||(*it>='A' && *it<='Z')||(*it>='a' && *it<='z')) {
// it was a valid char
++it;
} else {
// it was an invalid char, erase it
it = word.erase(it);
}
}
// if the word still has some characters in it, make it count:
if(word.size()) {
++words;
letters += word.size();
std::cout << '\'' << word << "'\n"; // for debugging
}
}
std::cout << "Number of words " << words << "\n";
std::cout << "Number of letters " << letters << "\n";
std::cout << "The average number of letters per word is "
<< static_cast<float>(letters) / words << '\n';
return 0.f; // not sure what you are supposed to return, but since the function
// signature says that you should return a float, you must return a float.
}
int main() {
checkString(" Hello !!! World, now let's see if it works. ");
}
#包括
#include//std::stringstream
#include//std::string
//使用std::string而不是char[]
浮点校验字符串(const std::string和string){
//将字符串放入stringstream以逐字提取
std::istringstream是(字符串);
无符号字=0;
无符号字母=0;
字符串字;
//从stringstream中一次提取一个单词:
while(is>>word){
//删除无效字符:
for(auto it=word.begin();it!=word.end();){
//不要使用幻数。请在代码中输入字符文字,以便
//每个人都明白你的意思
如果(**>0'和& =‘a’& *=‘a & & *it>p>我想补充一个答案。这个答案是基于“更现代”的C++和算法的使用。你想解决3个任务:
检查字符串是否正常并符合您的期望
计算给定字符串中的字数
数一数字母的数目
计算单词/字母的比率
对于所有这些,您可以使用C++标准库中的生存算法。在附加的示例代码中,您将看到每个任务的一个内衬。
这些陈述很简单,所以我不会再多解释了。如果有问题,我很乐意回答
请参见此处一个可能的示例代码:
#include <iostream>
#include <string>
#include <iterator>
#include <regex>
#include <algorithm>
#include <tuple>
#include <cctype>
std::regex re("\\w+");
std::tuple<bool, int, int, double> checkString(const std::string& str) {
// Check if string consists only of allowed values, spaces or alpha numerical
bool stringOK{ std::all_of(str.begin(), str.end(), [](const char c) { return std::isalnum(c) || std::isspace(c); }) };
// Count the number of words
int numberOfWords{ std::distance(std::sregex_token_iterator(str.begin(),str.end(), re, 1), {}) };
// Count the number of letters
int numberOfLetters{ std::count_if(str.begin(), str.end(), isalnum) };
// Return all calculated values
return std::make_tuple(stringOK, numberOfWords, numberOfLetters, static_cast<double>(numberOfWords)/ numberOfLetters);
}
int main() {
// Ask user to input string
std::cout << "Please enter a sentence:\n";
// Get string from user
if (std::string str{}; std::getline(std::cin, str)) {
// Analyze string
auto [stringOk, numberOfWords, numberOfLetters, ratio] = checkString(str);
// SHow result
std::cout << "\nString content check: " << (stringOk ? "OK" : "NOK") << "\nNumber of words: "
<< numberOfWords << "\nNumber of letters: " << numberOfLetters << "\nRatio: " << ratio << "\n";
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
std::regex re(“\\w+”);
std::tuple checkString(const std::string和str){
//检查字符串是否仅由允许的值、空格或字母数字组成
boolstringok{std::all_of(str.begin(),str.end(),[](const char c){return std::isalnum(c)| | std::isspace(c);});
//数一数字数
int numberOfWords{std::distance(std::sregx_token_迭代器(str.begin(),str.end(),re,1),{});
//数一数字母的数目
int numberOfLetters{std::count_if(str.begin(),str.end(),isalnum)};
//返回所有计算值
return std::make_tuple(stringOK、numberOfWords、numberOfLetters、static_cast(numberOfWords)/numberOfLetters);
}
int main(){
//要求用户输入字符串
std::cout建议:避免使用神奇的数字。不要写48
,使用文字'0'
。我建议您将标准库视为程序的基础。@VorpalSword为此引入正则表达式听起来有点过头了,OP可能会花更多的时间试图理解这一点,而不是用更多的约定来解决这一问题我不能用额外的方法libraries@OmerTesler这不是一个额外的库,它是C++标准的一部分。