Hello,
I have to write a function that takes in 2 parameters: a C-style string, and an integer value. The value determines how many slots each letter in the string will be rotated: positive means that many to the right and negative to the left. I can't use any libraries. I know that I can use the Reverse function and break up the arrays I'm just not quite sure how to write it all out in code. Thank you!
Create second string that's the same size as the first. Copy each character from the original string to the appropriate position in the new string. When you're done, copy the new string back over the original and delete the new string.
A function should ideally do exactly one task. This rotator function does not need to do I/O, so no libraries are required within the function. But you are right that one should pedantically harass the teacher, if the main() should do any output.
Okay here is an example. assume the string str is currently storing "The quick brown fox" a function call could look like this to rotate is 7 spaces to the right: RotateString(str, 7) // str is now "own foxThe quick br"
I really have no code besides a messed up version of the Reverse function. I dont know how to adjust it to take in a character array instead of a number array. I am so terrible at c plus plus. :/
void Reverse(char string[], int size) {
int temp, i;
for (i=0; i < size/2; i++){
temp = string[size-i-1];
string[size-i-1] = string[i];
string[i] = temp;
}
Your Reverse has array "string" that has elements of type char. The "temp", however, has type int. Would it not be more intuitive to store value from (and restore to) the array into a char variable?
You don't need to know much C++ for this task, but you need to think. dhayden did suggest the use of dynamically allocated array as temporary storage. A smart idea, but IMHO one should learn the use of standard library containers before learning the raw dynamic memory management.
It is good to break the problem down to smaller, simpler tasks. This problem can be solved so. Less efficient, but efficiency is not the goal; thinking is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
// This function rotates the C-string in text left by one position
void oneleft( char text[] )
{
// write code here
}
int main() {
char text[] = "The quick brown fox";
std::cout << '#' << text << "#\n"; // before
oneleft( text );
std::cout << '#' << text << "#\n"; // after
return 0;
}
Write the oneleft for us. Rotating by just one is much easier, isn't it?
So I believe i could write one function but call it a couple of times to get the desired amount of rotations? I'm thinking i could take the character array and reverse all but the last character and then take that array and reverse the whole thing. if the first part is reversed independently that would leave the one new character in the front! I think... The problem is I don't know what arguments to use for the particular type of rotation I want to perform. how would i put in another parameter to the reverse function so that it only reverses the beginning?