【问题标题】:How to move word in a circular motion in a string?如何在字符串中以圆周运动移动单词?
【发布时间】:2018-10-15 13:42:33
【问题描述】:

我有一个包含 X 个单词的字符串(每个单词之间有一个空格)我必须根据用户插入的数字以圆周运动将单词向左移动。例如:

"hi my name is aviv and",

用户输入了2。 "name is aviv and hi my" 我正在寻找可重复但我找不到的合法性。

感谢您的指导。最重要的是,我不能使用内置库


更新: 我看到有图书馆的例子,我不能使用任何图书馆。 所以到目前为止我所做的。 我写了一个函数,它从用户那里获取一个字符串和一个数字,然后向左移动。 在将字符串发送到函数之前,我尝试计算需要移动的字符数。

我的输出是 - “name is avivhi my” 关于功能: 当它得到一个没有空格的字符串时效果很好。

这是我的代码:

int main()
{
    char str[] = "hi my name is aviv";
    char str2[] = "hi my name is aviv";
    int CountSpace = 0, CountWord = 0;
    int Size = 18, flag = 0;
    int MoveLeft, Index = 0;
    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            CountSpace++;
        }    
    }

    CountWord = CountSpace + 1;//Understand how many words there are in a string.
    cin >> MoveLeft;

    if (MoveLeft >= CountWord)//
    {
        MoveLeft = (MoveLeft - ((MoveLeft / CountWord) * CountWord));//the size of movment;//To reduce the amount of moves if there is such a possibility
    }

    for (int i = Size - 1; i >= 0; i--)
    {
        if (str[i] == ' ')
        {
            flag++;
        }
        if (flag == MoveLeft)
        {
            Index = Size - 1 - (i + 1);//That's the amount of characters I have to move    
            break;
        }
    }    
    MoveLeft = Index;
    //This code belongs to the function that accepts a string and the amount to move the characters
    for (int i = 0; i < Size; i++)
    {
        if (i + MoveLeft < Size)
        {
            str[i] = str2[i + MoveLeft];
        }
        else
        {
            str[i] = str2[(i + MoveLeft) - Size];
        }
    }
    cout << "Move Left: " << MoveLeft << endl << str << endl << str2 << endl;
    return 0;
}

【问题讨论】:

  • 将单词存储在数组中并移动它们的索引。
  • 到目前为止你有什么尝试?发布您的代码。

标签: c++ string algorithm rotation stdstring


【解决方案1】:

如果您可以使用&lt;algorithm&gt; 中的std::rotate(),这很容易做到。使用std::stringstream 解析单词并存储到std::vector。然后将shif 直接应用于向量。

样本输出:https://www.ideone.com/rSPhPR

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

int main()
{
  std::vector<std::string> vec;
  std::string str = "hi my name is aviv and";
  std::string word;
  std::stringstream sstr(str);

  while(std::getline(sstr, word,' '))
    vec.emplace_back(word);

  int shift;
  std::cout << "Enter the Shift: "; 
  std::cin >> shift;

  std::rotate(vec.begin(), vec.begin() + shift, vec.end());
  for(const auto& it: vec)
    std::cout << it << " ";
  return 0;
}

【讨论】:

  • 我不能使用所有的库
  • @aviv.L 然后你需要逐字逐句解析和操作代码。
【解决方案2】:

这是一个sn-p:

#include <iostream>
#include <string>
#include <sstream>        
using namespace std;    
#define MaxWords 10

int main()
{
    stringstream ss;
    ss.str("hi my name is aviv and");

    string str[MaxWords];
    int i;
    for (i =0; std::getline(ss, str[i],' ');i++ )
    {
        cout << str[i] << " ";
    }

    int n;
    cout << "\nEnter pos to split : ";
    cin >> n;

    for (int j = n; j <= i; j++)
    {
        cout << str[j] << " ";
    }

    for (int j = 0; j < n; j++)
    {
        cout << str[j] << " ";
    }

    cout << endl;
    return 0;
}

输出:

【讨论】:

    【解决方案3】:

    一个可能的答案,我强烈建议使用vectors 而不是常规数组,它更简单且更具动态性,但我没有使用它,因为你说你不能使用内置库。

    #include <iostream>
    #include<string>
    using namespace std;
    
    int main() {
      string a[10000];
      int counter = 0;
      string b = "hi my name is aviv and";
      string temp = "";
      int userNum = 2;
      for(int i=0;i<b.length() ; i++){
        if(b[i]!=' '){
          temp+=b[i];
        }
        else if(b[i]==' ' && temp.length()){
          a[counter]= temp;
          temp = "";
          counter++;
        }
      }
    
      if(temp.length()){
        a[counter] = temp;
      }
    
      for(int i=userNum;i<=counter+userNum;i++){
        cout<<a[i%(counter+1)]<<endl;
      }
    }
    

    【讨论】:

      【解决方案4】:

      这里有一个提示:

      vector<string> words = Your_Code_To_Split_Input_Into_Words();
      int count = words.size();
      int shift = Your_Code_To_Read_Users_Input();
      
      // print the sentence with the rotation specified by shift
      for (int i = 0; i < count; i++)
      {
          int shifted_index = (i + shift) % count;  // modulo math implements circular rotation
          string spacing = (i == 0) ? "" : " ";     // add a space before each word, except first word
          cout << spacing << words[shifted_index];
      }
      cout << endl;
      

      【讨论】:

      • 我不能使用vector和
      • 然后对string使用char数组,对vector使用char*数组。我的回答的重点是你需要实现一个类似于我的for 循环。 for 循环的每次迭代都会计算 shifted_index = (i + shift) % count,用于索引要打印的正确单词。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2017-04-12
      • 2012-08-08
      相关资源
      最近更新 更多