0

Hello I am trying to write a function that converts a string to lowercase by using a pointer instead of a return value.

void makeLowerCase(std::string* in){
//for loop tolower(char from *in);}

but I have no idea how to get each char out of my pointer to use tolower() with, or how to get how many chars are in the string as

*in.length() 

and

sizeof(*in)/sizeof(char)

don't work for it. The former I get an error on the use of a pointer, the latter I get the same return value for sizeof(*in) so I don't even know how I would end my for loop.

ArtoriusIV
  • 9
  • 1
  • 6
  • Why not use a reference instead? `void makeLowerCase(std::string& in)` – DeathTails Jan 27 '16 at 15:33
  • If you are leveraging C++, you should use the libraries given inside of your function, but... outside of that statement, pass in the address of the `std::string` and then `in` will be a pointer you can manipulate. – Fallenreaper Jan 27 '16 at 15:34
  • Use `->` instead of `.` when working with pointers. – Greg M Jan 27 '16 at 15:35

4 Answers4

1

Instead of passing by pointer and dealing with pointer syntax you can pass the string by reference and then you can use it just like a normal std::string. If you have to use a pointer then you can either use

in->length();

or

(*in).length();

The parentheses are required in the second case as . has a higher precedence then *.

As for transforming the string to lower case you can use the built in functions from <algorithm> and and that would give you

void makeLowerCase(std::string& in)
{
    std::transform(in.begin(), in.end(), in.begin(), ::tolower);
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

C++ has a shortcut to get the member of an object pointed to by a pointer:

in->length()

For accessing characters, use parentheses:

(*in)[i]
John Sensebe
  • 1,386
  • 8
  • 11
0
*in.length() 

does not work because . has a higher precedence than *. Use parantheses:

(*in).length() 

sizeof(*in)/sizeof(char)

is the same as

sizeof(*in) / 1

because sizeof(char) == 1. sizeof(*in) == sizeof(std::string), so this yields the size of the std::string object itsself, not the string of characters, which is implemention-defined.


This information, in combination with iterators, for_each, and lambdas, make for a pretty three-liner without any functions:

#include <cctype>

...

for (char& c : str)
    c = std::tolower(c);

Notes:

  • Use references instead. They look better and are easier usable. Pointers should only be used in C++ for low-level stuff or when there's no way to cut them out.
cadaniluk
  • 15,027
  • 2
  • 39
  • 67
0

For pointers you would use the pointer operator. So that would be

in->length();

However a naked loop is not the ideal way (nor is using pointers to be honest).

A better way would be to use iterators to iterate through the string and convert it that way.

for (auto it=in->begin(); it!=in->end(); ++it) {
    *it = std::tolower(*it);
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
graham.reeds
  • 16,230
  • 17
  • 74
  • 137