Home > Enterprise >  How to use pointer to string in cpp?
How to use pointer to string in cpp?

Time:11-20

I am studying pointers in C . I have studied call by value and call by reference concept. I am trying to create a function to reverse a string which accepts a pointer to string and the size of string. The code is as follow

void reverse(string* str, int size)
{

    int start = 0;
    int end = size - 1;

    while(start < end)
    {
        swap(*str[start  ], *str[end--]);
    }
}


int main()
{
    
    string str = "Something";
    
    reverse(&str, str.length());

    cout << "Reversed string: " << str << endl;

    return 0;
}

I am getting this error:

error: no match foroperator*’ (operand type is ‘std::string’ {aka
‘std::__cxx11::basic_string’})
   12 |         swap(*str[start  ], *str[end--]);

I don't want to use the character array, is there way to do it? Someone please explain, what's wrong in my code. Thank you.

CodePudding user response:

Here is the simple fix. You don't need to change anything except a few lines.

#include <iostream>
#include <algorithm>
#include <cstring>


void reverse( std::string* str ) // no need to pass size to this function
{

    int start = 0;
    int end = str->length() - 1; // get the length of str like this

    char* ptrToCharArray =  const_cast<char*>( str->c_str() ); // gets the pointer to str's internal buffer

    while ( start < end )
    {
        std::swap( ptrToCharArray[start  ], ptrToCharArray[end--] ); // no need to use * operator anymore
    }
}


int main()
{
    
    std::string str = "Something";
    
    reverse( &str );

    std::cout << "Reversed string: " << str << std::endl;

    return 0;
}

Output is:

Reversed string: gnihtemoS

Hopefully, this helps you.

CodePudding user response:

Just need a little bit of change in your code Change this *str[start ] to (*str).at(start )

  void reverse(string* str, int size)
    {
    
        int start = 0;
        int end = size - 1;
    
        while(start < end)
        {
           swap((*str).at(start  ),(*str).at(end--));
        }
    }
    
    
    int main()
    {
        
        string str = "Something";
        
        reverse(&str, str.length());
    
        cout << "Reversed string: " << str << endl;
    
        return 0;
    }

CodePudding user response:

Note that there is no need to pass the size of the string as an argument to the function. You can use the member function std::string::size for that purpose as shown below:

Version 1: Passing pointer to string as argument

#include <iostream>
#include <algorithm>

void reverse(std::string *str)
{
    
    int n=(*str).size()-1;//dereference the pointer and use size member function on the resulting string object
  for(int i=0;i<((*str).size()/2);i  ){
    //Using the swap method to switch values at each index
    std::swap((*str).at(i),(*str).at(n)); //note this can also be written as std::swap((*str)[i],(*str)[n]);
    n = n-1;

  }
}
int main()
{
    std::string myString = "myString";
    reverse(&myString);
    
    std::cout<<"Reversed string is: "<<myString<<std::endl;

    return 0;
}

In version 1, *(str) gives us a std::string type object. Next we call size member function on this std::string object. Similarly we can call the std::string::at member function on this std::string object.

Version 2: Passing reference to string as argument

#include <iostream>
#include <algorithm>

void reverse( std::string &str)
{
    
    int n=str.size()-1;
    for(int i=0;i<(str.size()/2);i  ){
    //Using the swap method to switch values at each index
    std::swap(str.at(i),str.at(n));
    n = n-1;

  }
}
int main()
{
    std::string myString = "myString";
    reverse(myString);
    
    std::cout<<"Reversed string is: "<<myString<<std::endl;

    return 0;
}

  • Related