【问题标题】:C++ Program to Scramble String Phrases扰乱字符串短语的 C++ 程序
【发布时间】:2014-12-12 04:17:34
【问题描述】:

我正在编写一个程序来打乱通过字符串读入的行。这些短语是:

交替的内角

勾股定理

直角三角形

底角

边角边

我想打乱每个单独的单词,而不是整个短语,而是让每个短语单独一行(使用 getline 函数)。在每个短语之后,我将输出与短语长度相对应的下划线,每个下划线之间有空格,下划线后的双空格对应短语中单词的最后一个字母。

我要单独打乱单词的功能:

void scramble (apstring w)

{
srand (time(NULL));
apstring orig = w;
for(int i=0; i<w.length(); i++)
{
    int newSpot=rand()%w.length();
    char temp = w[i];
    w[i]=w[newSpot];
    w[newSpot]=temp;
}
while (orig == w);

我想知道如何在每一行中搜索

之后的空格
        getline(fin, phrase);

并通过扰码器单独发送每个单词,但在输出的短语中输出正确的间距,以及单词后正确数量的下划线。我想我需要将单词读入子字符串,并可能使用以下内容“清除”字符串:

apstring purgeString(apstring word)
{
apstring newWord = "";
for (int i = 0; i < word.length(); i++)
{
    if (isalpha(word[i]))
        newWord += word[i];
}
return newWord;
}

任何帮助或建议都将不胜感激,这已经困扰了我一段时间了。

【问题讨论】:

  • 附注:您应该只在程序运行开始时调用 srand(time(NULL)) 一次,而不是在 scramble() 内部。否则,每次调用 scramble() 时,您可能会得到相同的随机值序列(假设您的程序在不到一秒的时间内完成)。

标签: c++ string


【解决方案1】:

好吧,也许是这样的(伪代码,因为我不熟悉 apstring 类):

apstring theWholeLine = "This is a sentence with some words";
apstring curWord;
for (int i=0; i<theWholeLine.length(); i++)
{
   char nextChar = theWholeLine[i];

   if (isalpha(nextChar)) 
   {
      curWord += nextChar;
   }
   else if (curWord.length() > 0)
   {
      cout << "Time to scramble the next word: " << curWord << endl;
      curWord = "";
   }
}
if (curWord.length() > 0)
{
   cout << "Time to scramble the final word: " << curWord << endl;
}

(当然,您可以用代码替换或增加 cout 行,以进行适当的每字加扰和打印)

【讨论】:

    【解决方案2】:

    您可以使用您的phrase 创建一个std::stringstream 并从中读取每个单词(空格分隔),如下所示:

    std::stringstream ss(phrase);
    std::string word;
    while (ss >> word) {
       // ... process word
    }
    

    我注意到您正在处理单个字符和 C 函数。虽然这不一定是一个坏方法,但我将退后一步,让 C++11 完成繁重的工作:

    #include <algorithm>
    #include <random>
    #include <string>
    #include <sstream>
    
    // obtain a seed from a random_device for random number generator
    std::random_device seed;
    std::minstd_rand rng(seed());
    
    std::string shufflePhrase(const std::string& phrase)
    {
        std::string shuffledPhrase;
        std::string underscores;
        std::stringstream ss(phrase);
        std::string word;
    
        while (ss >> word) {
    
            // <algorithm> has a nice shuffle function that works on all iterable types
            std::shuffle(word.begin(), word.end(), rng);
            shuffledPhrase += word + ' ';
    
            // Add an underscore and space for each letter in the word
            for (const auto& letter : word) {
                underscores += "_ ";
            }
            underscores += ' ';
        }
    
        // Combine our shuffled phrase and underscores. We strip off the last 2 spaces.
        return shuffledPhrase + underscores.substr(0, underscores.size()-2);
    }
    

    现在我们有了这个函数,我们可以这样调用它:

    std::cout << shufflePhrase("alternate interior angles") << std::endl;
    

    可能的输出:

    nateaerlt ietnorri nlages _ _ _ _ _ _ _ _ _  _ _ _ _ _ _ _ _  _ _ _ _ _ _
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多