c++ - Reverse a string with pointers -


this question has answer here:

this amateur question. searched other posts topic, found lots of results, yet understand concepts behind solution.

this practice problem in c++ book. not assigned homework. [instructions here][1] .

what do:

string input; getline(cin, input); //get user's input.  int front = 0; int rear;  rear = input.size(); 

what problem wants me do

string input; getline(cin, input); //get user's input.  int* front = 0; int* rear;  rear = input.size(); 

error: value of type "size_t" cannot assigned entity of type int*

this makes sense me, cannot assign 'address' of int value of int. questions are:

  • what correct way go this? should forget initializing front* or rear* ints? avoid together? if so, syntax of solution?
  • why problem want me use pointers this? it's clear horrible usage of pointers. without pointers complete problem in 30 seconds. it's frustrating.
  • i don't see advantage ever using pointers aside doing returning array using pointers.

thanks guys. know users did research first. i'm irritated concept of pointers right vs. using actual variable itself.

posts topic i've read:

string.size() not return pointer - returns size_t.

to revert string try instead:

string original = "sometext";  // original string string reversed = original;    // make sure reversed string has same size original string  size_t x = original.size();    // size of original string  (size_t = 0; < x; i++)        // loop copy end of original start of reversed {     reversed[i]=original[x-1-i]; } 

if (for strange reason) needs pointers try this:

string input; getline(cin, input); //get user's input.  char* front = &input[0]; char* rear = &input[input.size()-1]; 

but not use pointers string. no need it.


Comments