Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C+中的简单句子中计算单词和数字+; 我是C++初学者(上了几节课,然后没有C++,几个月后再开始),我试图计算一个简单句子中的单词数,然后计算同一句子中的数字个数。为了计算字数,我使用: int countWord(char *word) { int counter = 0; char *words = strtok(word, " 0123456789-"); while (words != NULL) { counter++; words = strtok(NULL, " 0123456789-"); } return counter; }_C++ - Fatal编程技术网

如何从C+中的简单句子中计算单词和数字+; 我是C++初学者(上了几节课,然后没有C++,几个月后再开始),我试图计算一个简单句子中的单词数,然后计算同一句子中的数字个数。为了计算字数,我使用: int countWord(char *word) { int counter = 0; char *words = strtok(word, " 0123456789-"); while (words != NULL) { counter++; words = strtok(NULL, " 0123456789-"); } return counter; }

如何从C+中的简单句子中计算单词和数字+; 我是C++初学者(上了几节课,然后没有C++,几个月后再开始),我试图计算一个简单句子中的单词数,然后计算同一句子中的数字个数。为了计算字数,我使用: int countWord(char *word) { int counter = 0; char *words = strtok(word, " 0123456789-"); while (words != NULL) { counter++; words = strtok(NULL, " 0123456789-"); } return counter; },c++,C++,数字计数器基本上是一样的,只是我用字母表代替整数 char *num = strtok(number, " abcdefghijklmnopqrstuvwxyz"); 我的主要意见是: int main() { char str[] = "what time is 88 it 99today"; cout << "words = " << countWord(str) << " " << "numbers = " <<

数字计数器基本上是一样的,只是我用字母表代替整数

char *num = strtok(number, " abcdefghijklmnopqrstuvwxyz");
我的主要意见是:

int main()
{
    char str[] = "what time is 88 it 99today";

    cout << "words = " << countWord(str) << " " << "numbers = " <<
    countNum(str) << endl;          

    system("pause");

    return 0;
}
intmain()
{
char str[]=“今天几点”;
cout问题在于用空字符标记原始字符串中标记的结尾。引用自:

如果找到这样的字符,它将被空字符“\0”替换,并且指向以下字符的指针将存储在静态位置以供后续调用

注意:此函数具有破坏性:它在字符串str的元素中写入“\0”字符。特别是,字符串文本不能用作strtok的第一个参数

在你的情况下,这条线

cout << "words = " << countWord(str) << " " << "numbers = " <<
countNum(str) << endl;
相反的情况会发生


一种解决方案是在使用
strtok
时使用原始字符串的副本,例如使用
strtok(strdup(str))每次使用更好的标准,如使用C++等库函数,我确信使用纯C++有很多的文字计算解决方案。

< P>弗拉德已经为你的C风格代码提交了一个很好的答案。我的答案是演示使用更多的C++库来帮助你移动事物:

#include <iostream>
#include <string>
#include <vector>
#include <regex>

int main() {
    // The main string.
    std::string str = "what time is 88 it 99today"; 
    // Space is your delimiter
    std::string delimiter = " ";
    // Create a regex string for matching numbers, including floating point.
    std::regex number_chars(std::string("[0123456789.]+"));

    // A lambda function to help tokenize strings.
    // Returns a vector of substring tokens.
    // The internal code is taken from an answer on Stack Overflow.
    auto tokenizer = [](std::string s, std::string delimiter) {
        size_t pos = 0;
        std::string token;
        std::vector<std::string> tokens;

        while (pos = (s.find(delimiter))) {
            token = s.substr(0, pos);
            tokens.push_back(token);
            s.erase(0, pos + delimiter.length());

            if (pos == std::string::npos)
                break;

        }
        return tokens; 
    };

    // Apply the lambda.
    auto tokens = tokenizer(str, delimiter);

    // Output your tokens.
    for (auto it : tokens) {
        std::cout << it << "\n";    
    } std::cout << "\n";

    // Output tokens that are numbers.
    for (auto it : tokens) {
        if (std::regex_match(it, number_chars)) {
            std::cout << "String: " << it << " is a number.\n";    
        }
    }
    return 0;
}
#包括
#包括
#包括
#包括
int main(){
//主弦。
std::string str=“今天几点”;
//空格是您的分隔符
std::字符串分隔符=”;
//为匹配的数字(包括浮点)创建正则表达式字符串。
std::regex number_chars(std::string(“[0123456789.]+”);
//用于帮助标记字符串的lambda函数。
//返回子字符串标记的向量。
//内部代码取自堆栈溢出时的应答。
自动标记器=[](标准::字符串s,标准::字符串分隔符){
大小\u t pos=0;
字符串标记;
std::向量标记;
while(pos=(s.find(分隔符))){
令牌=s.substr(0,位置);
代币。推回(代币);
s、 擦除(0,pos+分隔符.length());
if(pos==std::string::npos)
打破
}
归还代币;
};
//应用lambda。
自动标记=标记器(str,分隔符);
//输出您的令牌。
用于(自动it:令牌){

STD:如果使用C++字符串代替C字符串,那么看起来似乎更容易。它看起来比C++更适合C。只是字面意思,即“单词”(在这种情况下)是“什么”、“时间”、“是”、“它”、“今天”。。我还没有看过C。在我上的两门课中,都没有讨论过如何计算单词或数字,只是计算字符。当我寻找如何计算单词或数字时,我只找到了strtok(),而且解释是有限的(除非有限的解释是它所需要的)@ JustinMangawang,你甚至不在C++编程。这更像是用C++编译器编译的C。如果你要用C++来做这个,使用C++库。谢谢,我一直想知道我的错误是否来自我使用STRtok()的STR(如我所说的,我只是在学习Strutk)。我将对如何实现这一点进行更多的研究。@ JustinMangawang欢迎。一个快速而肮脏的解决方案是使用<代码> Strtok(Strup(STR))< /C> >,每次你将使用一个字符串的副本。但是尝试学习适当的C++库特征,并使用<代码> STD::String 结合算法。StruUp()有效。我肯定会更多地研究这些功能。到目前为止,从我所读到的内容来看,通过使用名称空间std;,我忽略了使用“std::string”,但我更倾向于接受您的建议和其他建议。请,请,请不要使用“使用名称空间std”@Mailerdaimon?我没有使用任何,这只是一个从OP问题粘贴代码的示例。感谢您的解释。我将学习您在这里输入的很多内容(例如正则表达式)这已经向我介绍了很多新的材料。@ Juthimm。一个好的C++指南至少可以指导你使用<代码> STD::String
类来进行字符串操作。对于大多数事情,你不应该使用C字符串(除非你使用Unicode),但是有库…
...operator<<(operator<<(cout, "words"), countWord(str))...
cout << "words = " << countWord(str) << " ";
cout << "numbers = " << countNum(str) << endl;
#include <iostream>
#include <string>
#include <vector>
#include <regex>

int main() {
    // The main string.
    std::string str = "what time is 88 it 99today"; 
    // Space is your delimiter
    std::string delimiter = " ";
    // Create a regex string for matching numbers, including floating point.
    std::regex number_chars(std::string("[0123456789.]+"));

    // A lambda function to help tokenize strings.
    // Returns a vector of substring tokens.
    // The internal code is taken from an answer on Stack Overflow.
    auto tokenizer = [](std::string s, std::string delimiter) {
        size_t pos = 0;
        std::string token;
        std::vector<std::string> tokens;

        while (pos = (s.find(delimiter))) {
            token = s.substr(0, pos);
            tokens.push_back(token);
            s.erase(0, pos + delimiter.length());

            if (pos == std::string::npos)
                break;

        }
        return tokens; 
    };

    // Apply the lambda.
    auto tokens = tokenizer(str, delimiter);

    // Output your tokens.
    for (auto it : tokens) {
        std::cout << it << "\n";    
    } std::cout << "\n";

    // Output tokens that are numbers.
    for (auto it : tokens) {
        if (std::regex_match(it, number_chars)) {
            std::cout << "String: " << it << " is a number.\n";    
        }
    }
    return 0;
}