Skip to main content
added 14 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

BelowHere is a program I've written to reverse a string in C++ using std::string std::string (SO tells me this should be used as opposed to C-style strings/char arrays).

This works, but I have a few questions.:

QuestionsQuestions:

  1. What is good about this code? (Soso that I can continue to do this for other programs.)?

  2. What is bad about this code? (Be brutal in your assessment.) Specifically Specifically, I have doubts about the best approach to take while writing functions functions 2 and 3.

    const string& stringOps::displayString(const stringOps &a)
    {
        //cout << a.arr << endl;
        return a.arr;
    }

I tried doing this with return type as "void"void and "cout"cout within the function, but apparently there's going to be an error for this with iostreamiostream. So is returning const string&const string& a good way to do this? I I passed by constconst reference because I do not change the object within the function.

    stringOps stringOps::reverseStr(stringOps &a)
    {
        reverse(a.arr.begin(), a.arr.end());
        return a;
    }

a. This again takes the argument of type stringOps object by reference. And directly manipulates the string. However, if I were to pass this argument as a const, how do I go about this?

  1. This again takes the argument of type stringOps object by reference and directly manipulates the string. However, if I were to pass this argument as a const, how do I go about this?

  2. Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly.

b. Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly. Header The header would look something like this -:

    string stringOps::reverseStr(const stringOps &a)

Do I declare static string inside the function and return it? I I tried using a local non-static string (which would be destroyed outside the function, but still for experiment purposes) and I got an error that said read-only variable is not assignable.:

read-only variable is not assignable

I'm assuming this is because it's a constconst reference.

Below is a program I've written to reverse a string in C++ using std::string (SO tells me this should be used as opposed to C-style strings/char arrays).

This works, but I have a few questions.

Questions:

  1. What is good about this code? (So that I can continue to do this for other programs.)

  2. What is bad about this code? (Be brutal in your assessment.) Specifically, I have doubts about the best approach to take while writing functions 2 and 3.

const string& stringOps::displayString(const stringOps &a)
{
    //cout << a.arr << endl;
    return a.arr;
}

I tried doing this with return type as "void" and "cout" within the function, but apparently there's going to be an error for this with iostream. So is returning const string& a good way to do this? I passed by const reference because I do not change the object within the function.

stringOps stringOps::reverseStr(stringOps &a)
{
    reverse(a.arr.begin(), a.arr.end());
    return a;
}

a. This again takes the argument of type stringOps object by reference. And directly manipulates the string. However, if I were to pass this argument as a const, how do I go about this?

b. Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly. Header would look something like this -

string stringOps::reverseStr(const stringOps &a)

Do I declare static string inside the function and return it? I tried using a local non-static string (which would be destroyed outside the function, but still for experiment purposes) and I got an error that said read-only variable is not assignable. I'm assuming this is because it's a const reference.

Here is a program I've written to reverse a string in C++ using std::string (SO tells me this should be used as opposed to C-style strings/char arrays):

Questions:

  1. What is good about this code (so that I can continue to do this for other programs)?

  2. What is bad about this code? (Be brutal in your assessment.) Specifically, I have doubts about the best approach to take while writing functions 2 and 3.

    const string& stringOps::displayString(const stringOps &a)
    {
        //cout << a.arr << endl;
        return a.arr;
    }

I tried doing this with return type as void and cout within the function, but apparently there's going to be an error for this with iostream. So is returning const string& a good way to do this? I passed by const reference because I do not change the object within the function.

    stringOps stringOps::reverseStr(stringOps &a)
    {
        reverse(a.arr.begin(), a.arr.end());
        return a;
    }
  1. This again takes the argument of type stringOps object by reference and directly manipulates the string. However, if I were to pass this argument as a const, how do I go about this?

  2. Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly.

The header would look something like this:

    string stringOps::reverseStr(const stringOps &a)

Do I declare static string inside the function and return it? I tried using a local non-static string (which would be destroyed outside the function, but still for experiment purposes) and I got an error that said:

read-only variable is not assignable

I'm assuming this is because it's a const reference.

Source Link
Raaj
  • 163
  • 5

Program to reverse a string using std::string

Background: I'm trying to learn C++ and I'm doing that by writing some programs by myself. I'm decent with C, but I'm having trouble translating to OOP paradigm. I'm reading from learncpp.com and a book called Thinking in C++.

I'm noticing that reading doesn't really help much as much as writing programs and running into issues and solving them.

Below is a program I've written to reverse a string in C++ using std::string (SO tells me this should be used as opposed to C-style strings/char arrays).

This works, but I have a few questions.

example_one.h

#ifndef example_one_h
#define example_one_h

#include<iostream>
#include<string>
#include<cstdio>

using namespace std;

class stringOps{
    
    private:
        string arr;
        int length;
    
    public:
        // Constructor - Destructor
        stringOps(string arr);
        ~stringOps();
        // Member functions
    
        //1. get length of string
        int getLength();
    
        //2. print string
        const string& displayString(const stringOps &a);
    
        //3. Reverse the string and return obj of type stringOps.
        stringOps reverseStr(stringOps &a);
    
};

#endif

example_one.cc

#include "example_one.h"

/* Constructor and Destructor */
stringOps::stringOps(string a):arr(a), length(a.size()) {
}

stringOps::~stringOps() {
}

/* Member functions */
// 1.  get Length
int stringOps::getLength()
{
    return arr.size();
}

//2. display string
const string& stringOps::displayString(const stringOps &a)
{
    //cout << a.arr << endl;
    return a.arr;
}

//3. reverse array of the object.
stringOps stringOps::reverseStr(stringOps &a)
{
    reverse(a.arr.begin(), a.arr.end());
    return a;
}


/*  -------------------------------- Main  --------------------- */
int main()
{
    stringOps strA("Hello");
    
    string x = strA.displayString(strA);
    printf("Original string: %s \n", x.c_str());
    
    int len = strA.getLength();
    printf("String length:%d \n",(len));
    
    strA.reverseStr(strA);
    string y = strA.displayString(strA);
    printf("Reverse string: %s \n", y.c_str());
    
    
    
    return 0;
}

Questions:

  1. What is good about this code? (So that I can continue to do this for other programs.)

  2. What is bad about this code? (Be brutal in your assessment.) Specifically, I have doubts about the best approach to take while writing functions 2 and 3.

Function 2:

const string& stringOps::displayString(const stringOps &a)
{
    //cout << a.arr << endl;
    return a.arr;
}

I tried doing this with return type as "void" and "cout" within the function, but apparently there's going to be an error for this with iostream. So is returning const string& a good way to do this? I passed by const reference because I do not change the object within the function.

Function 3:

stringOps stringOps::reverseStr(stringOps &a)
{
    reverse(a.arr.begin(), a.arr.end());
    return a;
}

a. This again takes the argument of type stringOps object by reference. And directly manipulates the string. However, if I were to pass this argument as a const, how do I go about this?

b. Motivation for me to do so is that I want to return a manipulated string that is new, and not manipulate the object that is coming in directly. Header would look something like this -

string stringOps::reverseStr(const stringOps &a)

Do I declare static string inside the function and return it? I tried using a local non-static string (which would be destroyed outside the function, but still for experiment purposes) and I got an error that said read-only variable is not assignable. I'm assuming this is because it's a const reference.