如何在字符串中以圆周运动移动单词?

时间:2018-05-05 04:02:47

标签: c++ string algorithm rotation

我有一个包含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;
}

4 个答案:

答案 0 :(得分:1)

这是一个提示:

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;

答案 1 :(得分:1)

一个可能的答案,我强烈建议使用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;
  }
}

答案 2 :(得分:1)

如果您可以使用std::rotate()中的<algorithm>,那么这很容易实现。使用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;
}

答案 3 :(得分:0)

这是一个片段:

#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;
}

输出:

enter image description here