1

I have a problem with reversing my strings of digit 1 and 2.

Anyone can advise accordingly?

I'm trying to reverse using a tempholder1 and tempholder2 as function variables and via for loop.

I cout << digit1 << digit2; // but do not get anything at the compiler.

Edited for better efficiency

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;

/* GLOBAL DECLARATIONS */

/* FUNCTIONS */

// Reverse the string
void reverseString(string&);
// Transfer all the digits in a char array to an integer array
// Add two long integers
// Print out the long integers


int main()
{       
    string digit1, digit2;
    char again; // Y for menu
    do 
    {
        // Fetch Values
        cout << "Enter a string of digits: ";
        cin >> digit1;
        cout << "Enter a string of digits: ";
        cin >> digit2;

        // Reverse the string
        reverseString(digit1);
        reverseString(digit2);
        cout << digit1 << digit2;
        // Return sum of both values
        cout << "The sum is: ";

        // Do it again?
        cout << "Continue (Y/N)?: ";
        cin >> again;
    } while ((again=='y')||(again=='Y'));
}

// Reverse the string
void reverseString(string& digit)
{
    string tempholder1;

    // tempholder1 = reverse of digit1
    int k=0;
    for(int i=digit.length()-1;i>=0;i--)
    {
        tempholder1[k]=digit[i];
        k++;
    }

    digit=tempholder1;

}
9
  • 3
    Why does reverseString reverse two strings, rather than having it reverse one string and then call it twice? Commented Apr 25, 2017 at 12:39
  • @TimB i used string& digit1, string& digit2 (passed by reference) and void statement. so it should be fine doing 2 at the same time Commented Apr 25, 2017 at 12:40
  • But it's still a bad idea. You've duplicated code and made the function less generally useful. (What if you want to reverse 1 string, or 3?) Commented Apr 25, 2017 at 12:42
  • @TimB understood ill change the code. but anyway when i cout digit1 theres nothing being display. any advice? Commented Apr 25, 2017 at 12:43
  • 5
    Please take a look at std::string::operator[]. It "returns a reference to the character at the given position. No bounds checking is performed". Since your strings are empty, it returns a reference to Richie knows where which is undefinied behaviour. Commented Apr 25, 2017 at 12:48

3 Answers 3

1

In your reverseString function you are not initializing your string variable, as such what is the size of characters it can store? Then when you go through you are accessing individual elements of that string, what if tempholder1 only has a length of 1? you can't just access the second index and change it(since it could be invalid), the reason you can write things like:

myString = "SomeReallyLongString";

is because the string operator= is written to increase the number of chars it stores.

so in your function

// Reverse the string
void reverseString(string& digit)
{
    // Uninitialized variables are really bad!!! always always initialize
    // string tempholder1;
    string temphold = digit;

    // tempholder1 = reverse of digit1
    int k=0;
    for(int i=digit.length()-1;i>=0;i--)
    {
        temphold[k]=digit[i];
        k++;
    }

    digit=temphold;

}
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, string temphold; is initialized, using the default constructor of std::string. The actual problem is that temphold[k] performs an out-of-bound access (your copy initialization allocates the needed memory).
1

The main problem in your implementation is that you forgot to allocate memory in the temporal string, therefore it doesn't have the size you're expecting.

What about an in-place reverse? You'd avoid everything related to the use of a temporal string (copies, additional memory...)

void reverseString(std::string& str)
{
  const size_t n = str.length();
  for (size_t i = 0; i < n / 2; ++i) {
    std::swap(str[i], str[n - i - 1]);
  }
}

Comments

-1

to reverse a string i would do:

    std::string name{"Hello World"}; 

    for(auto i = name.rbegin(); i != name.rend(); ++i){
    std::cout << *i; 
    }
//Output: dlroW olleH 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.