Using Pointers to shift a word

This is the assignment. I am having trouble understanding pointers and cannot figure this one out! My C++ course is online and the professor does not help at all lol any help or advice will be awesome!


Using pointers, have the user input a 12 character word. Store this word in a char*.
Next ask the user for an integer.
Manipulate the char* to shift the word to the right by the integer amount:

>Enter a Word:
Hello World
>Enter a Number:
3

Your Shifted Word is:
rldHello Wo

>Enter a Word:
Teddy Bear
>Enter a Number:
6

Your Shifted Word is:
y BearTedd
There are many ways to do this, however since it is an assignment I'm assuming the most obvious thing.

Looping through the string literal back to front and copying the characters 3 elements back.

Writing example.
Last edited on
okay looks good! but Im still having trouble understanding.... specially this part
1
2
const int SHIFT = 3;
char str[6 + SHIFT] = "012345";

what is the function of SHIFT and where did it come from?

also which of these variables is the integer provided by the user?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   int shift;
   int length;
   char* str;

   cout << "Input string: " << endl;
   cin >> str;

   cout << "How many cells would you like to shift: " << endl;
   cin >> shift;

   length = strlen(str);
   realloc(str, length + shift);  //Must expand array to fit in extra 3 spaces

   for(int i = length; i >= 0; i--)
   {
      str[i + shift] = str[i];
      str[i] = ' ';
   }

   cout << str;


There we go.

length is used by "for" to begin from the last cell of the character array to the start. So "01234" would be at position 4 holding '5', going till 0. I can shifting the characters shift cells in front of the current cell. Resulting in all the characters shunting shift amount of times.
Last edited on
haha dude I'm trying to understand but I have really bad C++ background (online courses suck) and still don't get it...This is what I have so far... if you can explain again how to make the actual word shift using that loop it would be awesome


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>
#include <iostream>
#include <string>
using namespace std;


int main ()
{
char * inputSTR;
char * outputSTR;
string userword; 
int userint;

cout<< "please enter a 12 character word: "<<endl;
getline(cin,userword);
userword = inputSTR; 
cout<<"Thanks! now enter an integer!: ";
cin>>userint;


... wierd loop goes here?....






Ok, I'll explain with no code. :P

I can not multi space in quotes, I will use 0/zero for empty value. (I'm using interchangeably for space and NULL for simplicity)

-I have typed in the string: "abcd", length of 4
-Then I have said to shift these characters 3 spaces.
-Knowing these two things, I need an array to hold 4 characters + 3 extra spaces.
-Now the character array is: "abcd000", note 3 empty spaces

-I loop from 'd' to 'a', counter will be i, holding the current position in the array.
-Assign the empty character at the end with d, then make that value empty I get: "abc000d"
-Assign the empty character at the end with c, then make that value empty I get: "ab000cd"
-Assign the empty character at the end with b, then make that value empty I get: "000bcd"
-Assign the empty character at the end with a, then make that value empty I get: "000acd"
Last edited on
Alright! I understand what you are saying now I want to understand it in code... This loop part is what I dont get, I understand it makes the shift process from d to a...

1
2
3
4
 for(int i = length; i >= 0; i--)
   {
      str[i + shift] = str[i];
      str[i] = ' ';


but how do I make this shift all of the letters and get the final answer of 000acd? I understand I am missing many parts to finish this code, do I need more loops? if so how many? Also the string to be shifted is supposed to have a fixed length of 12 "Using pointers, have the user input a 12 character word..." shouldn't that affect the way to go about this?

Also this is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <iostream>
#include <string>
using namespace std;


int main ()
{
int aShift;
int length;
char* str;

   cout << "Please input a 12 character word: " << endl;
   cin >> str;

   cout << "Please input an integer to shift the word: " << endl;
   cin >> aShift;

   length = strlen(str);
   realloc(str, length + aShift); 

   for(int i = length; i >= 0; i--)
   {
      str[i + aShift] = str[i];
      str[i] = ' ';
   }

   cout << str;
}



In that case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>

using namespace std;

int main ()
{
   int aShift;
   int length;
   char str[12];
   char* newstr;

   cout << "Please input a 12 character word: " << endl;
   cin >> str;

   cout << "Please input an integer to shift the word: " << endl;
   cin >> aShift;

   length = strlen(str);
   newstr = new char[length + aShift];

   for(int i = length; i >= 0; i--)
   {
      newstr[i + aShift] = str[i];
   }

   cout << newstr;
}
Last edited on
Thanks! but as it is it does not seem to work... it says:

fatal error LNK1169: one or more multiply defined symbols found. Is there something missing?
closed account (zb0S216C)
In Turbine's latest post, he/she's included <string> twice.

Wazzak
Is there something missing?

Something is not missing, but rather, something is there too often. Quite the opposite :)
Well the <string> part I got... but that doesnt seem to be the issue lol
The code is still not working
error LNK2005: _main already defined
fatal error LNK1169: one or more multiply defined symbols found


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;


int main ()
{
   int aShift;
   int length;
   char str[12];
   char* newstr;

   cout << "Please input a 12 character word: " << endl;
   cin >> str;

   cout << "Please input an integer to shift the word: " << endl;
   cin >> aShift;

   length = strlen(str);
   newstr = new char[length + aShift];

   for(int i = length; i >= 0; i--)
   {
      newstr[i + aShift] = str[i];
   }

   cout << newstr;

}
I should really read what I do and do not , when copy and paste.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <string>
#include <iostream>
#include <cstring>

using namespace std;

int main ()
{
   const int LENGTH = 12;
   int aShift;
   char str[LENGTH];
   char* newstr;

   cout << "Please input a " << LENGTH << " character word: " << endl;
   cin >> str;

   cout << "Please input an integer to shift the word: " << endl;
   cin >> aShift;

   newstr = new char[LENGTH + aShift];
   memset(newstr, ' ', sizeof(newstr));

   for(int i = 0; i < LENGTH; i++)
   {
      newstr[i + aShift] = str[i];
   }

   cout << newstr;

   return 0;
}
Last edited on
Thanks!! It works perfect and thanks to you guys I now understand better the topic!

But one last thing! is there a way to make the code handle spaces like "hello world" without crashing? or can it only handle straight un-spaced words?
Last edited on
This line is wrong:

memset(newstr, ' ', sizeof(newstr));

newstr is of type char* so sizeof(newstr) == sizeof(char*) is going to be 4 bytes (for 32-bit systems).

You need

memset(newstr, ' ', LENGTH + aShift);

Andy


Text with spaces in it -- like "hello world" -- doesn't work as you're using cin to get the text, which extracts up to the first space by default.

You could switch the code to use cin.getline(), but I also think that the code isn't providing space for a terminating null. So if you enter a string of 12 chars you're going to hit problems.

I would prob. use getline(cin, str), where str is a string variable, and then check the length.

Andy
Yea thanks andy! but it still cannot hold more than one word like "hello world" I think it is not a problem since it clearly asks the user to plug a 12 character word... but anyways can this be made to handle such a phrase?
I've given the required information: see the documentation for istream::getline()

C strings always have one more character in memory than expected: the null teminator.

"Hello world" = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0'}

where '\0' is the terminating NULL.

So a buffer of 12 characters can only hold a string of 11 chars + the null terminator.
Thanks! you did give me the information I didnt see it when I replied

Anyways... what do you advice me to do for the code to work (cuz I just realized it is crashing every time and when it does not it does not shift the word)... should I start using strings and "check length"using an array?

Last edited on
Well never mind... haha I've had it with this code and its for tomorrow so Ill just turn it in as is...

Thanks all of you for the help
Topic archived. No new replies allowed.