Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I'm trying to write a program in C++ that takes the first character of users input and puts it to the end. Do program will do that for n tines

Eg.

User Input: 'stack'

'How many times should I rotate?'

User Input: 2

Output: 'ackst'

What I have tried:

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int K;

	string S;
	cout << "------------------------------------"<<endl;
	cout << "Enter a word: ";
	cin >> S;
	cout << "------------------------------------"<<endl;

	if(S.length() >1000)
	{
	cout << "------------------------------------"<<endl;
	cout << "Too many characters"<<endl;
	cout << "------------------------------------"<<endl;
	}
	
	if(S.length() ==1)
	{
	cout << "------------------------------------"<<endl;
	cout << "Not enough characters"<<endl;
	cout << "------------------------------------"<<endl;
	}
	
	if (S.length() <=1000 && 
  S.length() !=1)

        {
	cout << "Times of rotation: ";
	cin >> K
	}
}
Posted
Updated 7-Dec-18 11:46am
v2

Try this:

C++
if (S.length() <= 1000 && S.length() != 1)
{
	cout << "Times of rotation: ";
	cin >> K;

	int L = S.length();

	for (int ii = 0; ii < K; ii++)
	{
		char chFirst = S[0];

		for(int i = 0; i < L - 1; i++)
			S[i] = S[i+1];

		S[L - 1] = chFirst;
	}
			

	cout << S;
}


A short explanation:
- we save the first character
- loop n- times, where n the rotation is.
- inner loop: replace the first char with the second, the second with the third and so on
- set the first character to the and of the string
 
Share this answer
 
v2
Comments
Member 14081190 7-Dec-18 9:35am    
Thanks! Works just fine, also thank you for the short explanation. :)
Leo Chapiro 7-Dec-18 10:15am    
You are welcome! Consider to mark the answer as "accepted" if it works, it may help other users.
In an alternative approach, you could use string::substr
C++
#include <iostream>
#include <string>
using namespace std;


void rotate (string & s, size_t r)
{
  r %= s.length();
  if ( r == 0) return;
  s = s.substr(r,s.length()-r) + s.substr(0,r);
}

void show_decorated(const string & message)
{
  cout << "------------------------------------\n";
  cout << message << '\n';
  cout << "------------------------------------\n";
}

int main ()
{
  int K;
  string S;
  cout << "------------------------------------\n";
  cout << "Enter a word: ";
  cin >> S;
  cout << "------------------------------------\n";

  if( S.length() > 1000)
  {
    show_decorated( "Too many characters" );
    return -1;
  }
  if(S.length() <= 1)
  {
    show_decorated("Not enough characters");
    return -1;
  }

  cout << "Times of rotation: ";
  cin >> K;
  rotate(S,K);
  show_decorated(S);
  cout << endl;

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900