我该如何修复我的“for”问题?循环,以便将相同值的多个输出合并为一个输出(按字母将字符串分隔为长度)

发布于 2025-01-20 03:30:29 字数 984 浏览 3 评论 0原文

我试图将字符串输入分成单独的单词,然后在不使用 find 的情况下确定每个单词的长度,而仅使用下面列出的库。我已经成功创建了一个“for”循环来执行此操作,但是,它单独输出每个单词。我想将具有相同值长度的单词组合成一个输出,但尚未弄清楚。

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout<<"Enter a phrase: "<<endl;
    getline(cin, a);
    int count=0;
    

    for(int i = 0; i <= a.size(); i++){
        if(!isspace(a[i]) && a[i]){
            s += a[i];
            count;
        } 
        else if(!a[i] || isspace(static_cast<unsigned char>(a[i]))){
            count++;
            cout << s.length() << " characters long: " << count<<" words."<< endl;
            s = "";
        }
        else{
        count=0;
        }
    }

    return 0;
}

示例:

输入:“我希望我可以结合这个”

输出: 1 个字符长:1 个单词。 4 个字符长:2 个单词。 1 个字符长:3 个单词。 5 个字符长:4 个单词。 7 个字符长:5 个单词。 4 个字符长:6 个单词。

I'm trying to separate a string input into individual words, and then determine the length of each word without using find, and while only using the listed libraries below. I've managed to create a 'for' loop that does this, however, it outputs each word individually. I would like to combine words with the same value length into one single output, but haven't been able to figure it out yet.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout<<"Enter a phrase: "<<endl;
    getline(cin, a);
    int count=0;
    

    for(int i = 0; i <= a.size(); i++){
        if(!isspace(a[i]) && a[i]){
            s += a[i];
            count;
        } 
        else if(!a[i] || isspace(static_cast<unsigned char>(a[i]))){
            count++;
            cout << s.length() << " characters long: " << count<<" words."<< endl;
            s = "";
        }
        else{
        count=0;
        }
    }

    return 0;
}

example:

input: "I wish I could combine this"

output:
1 characters long: 1 words.
4 characters long: 2 words.
1 characters long: 3 words.
5 characters long: 4 words.
7 characters long: 5 words.
4 characters long: 6 words.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2025-01-27 03:30:29

快速而肮脏的方法是拥有一个数组,即输入字符串 + 1的长度(因为没有单词大于整个字符串)。每个索引将对应于单词长度,您可以相应地增加这些索引。然后在最后只打印非零长度。这样的事情:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout << "Enter a phrase: "<< endl;
    getline(cin, a);

    int sizes[a.length()+1] = {}; // array for the lengths of words

    for(int i=0; i<a.length()+1; i++) {

        if(!isspace(a[i]) && a[i]){
            s += a[i];
        } else if (!a[i] || isspace(static_cast<unsigned char>(a[i]))) {
            int index = s.length();
            sizes[s.length()]++; // incrementing the array at the given size
            s = "";
        }

    }

    for(int i=0; i<(sizeof(sizes)/sizeof(*sizes)); i++) {
        if(sizes[i]!=0)
            cout << "There are: " << sizes[i] << " words: " << i << " characters long\n";
    }
    
    return 0;
}

A quick and dirty way to do it would be to have an array which is the length of the input string + 1 (since no word could be larger than the entire string). Each index would correspond to a word length, and you could increment those accordingly. Then at the end just print the nonzero lengths. Something like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    string a;
    string s;
    
    cout << "Enter a phrase: "<< endl;
    getline(cin, a);

    int sizes[a.length()+1] = {}; // array for the lengths of words

    for(int i=0; i<a.length()+1; i++) {

        if(!isspace(a[i]) && a[i]){
            s += a[i];
        } else if (!a[i] || isspace(static_cast<unsigned char>(a[i]))) {
            int index = s.length();
            sizes[s.length()]++; // incrementing the array at the given size
            s = "";
        }

    }

    for(int i=0; i<(sizeof(sizes)/sizeof(*sizes)); i++) {
        if(sizes[i]!=0)
            cout << "There are: " << sizes[i] << " words: " << i << " characters long\n";
    }
    
    return 0;
}
鹿童谣 2025-01-27 03:30:29

我会使用 std::map 将每个单词的长度映射到该长度的单词数。

像这样:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <map>

int main()
{
    std::string a = "I wish I could combine this";
    std::string s;
    std::map<size_t, int> m;

    for (size_t i = 0; i < a.size(); i++) 
    {
        if (!std::isspace(a[i]) && a[i])
        {
            s += a[i];
        }
        else if (!a[i] || std::isspace(static_cast<unsigned char>(a[i])))
        {
            ++m[s.size()];
            s.clear();
        }
    }
    if (!s.empty())
    {
        ++m[s.size()];
    }

    for (std::map<size_t, int>::iterator it = m.begin(); it != m.end(); ++it)
    {
        std::cout << it->first << " characters long: " << it->second << " words.\n";
    }

    return 0;
}

在线示例

输出:

1 characters long: 2 words.
4 characters long: 2 words.
5 characters long: 1 words.
7 characters long: 1 words.

I would use a std::map to map the length of each word to the number of words of that length.

Like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <map>

int main()
{
    std::string a = "I wish I could combine this";
    std::string s;
    std::map<size_t, int> m;

    for (size_t i = 0; i < a.size(); i++) 
    {
        if (!std::isspace(a[i]) && a[i])
        {
            s += a[i];
        }
        else if (!a[i] || std::isspace(static_cast<unsigned char>(a[i])))
        {
            ++m[s.size()];
            s.clear();
        }
    }
    if (!s.empty())
    {
        ++m[s.size()];
    }

    for (std::map<size_t, int>::iterator it = m.begin(); it != m.end(); ++it)
    {
        std::cout << it->first << " characters long: " << it->second << " words.\n";
    }

    return 0;
}

Online Example

Output:

1 characters long: 2 words.
4 characters long: 2 words.
5 characters long: 1 words.
7 characters long: 1 words.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文