I have a string that contains X words (between each word there is a space) I have to move the words in a circular motion to the left according to the number that the user inserts. For example, "hi my name is aviv and", the user entered 2. "name is aviv and hi my" I'm looking for legality that repeats itself but I can not find.Thanks for the guidance Most importantly, I can not use built-in libraries

updating: I see there are examples with libraries, I can not use any library. So what I've done so far. I written a function that gets a string and a number from the user, to move left. Before sending the string to the function I try to calculate the amount of characters I need to move. My output is - "name is avivhi my" Regarding the function- When it gets a string without spaces it works great

this is my code-

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 Answers

1
selbie On

Here's a hint:

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
Zeyad Etman On

One possible answer, i highly recommend using vectors instead of regular arrays, it's easy and more dynamic, but i didn't use it because you said you can't use built-in libraries.

#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;
  }
}
0
seccpur On

Here's a snippet :

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

Output:

enter image description here

1
JeJo On

If you can make use of std::rotate() from <algorithm>, this is much easy to do with that. Parse the words using std::stringstream and store to std::vector. Then apply the shif directly to the vector.

Sample Output: 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;
}