0

I have an assignment that wants me to get a cstyle string (just 1 word) from the user then use a function to reverse its letters. The function has to take in 2 parameters, the first that is the destination string, and the other that is the source string. It will take whatever is in the source string, reverse it, then store the reversed version in the second string.

But everytime I compile, I typed in hello it prints out: \370\365\277\357\376. I just learned how to use pointers and cstyle strings so I don't really know how to use them and I think thats what is messing up my code. I'm dont understand it that well though so I cant figure out where I'm going wrong at.

Please let me know if you know what I'm doing wrong. Thank you!

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;

void IsPalindrome(char *cstring);
void Reverse(char *str2[], char *str1[]);
int main() {
    // user variables
    char str1[81];
    char reverse;
    char str2[81];

    strcpy (str2, str1);

    //prompt user to input string
    cout << "\nPlease input a string (80 chars max): ";
    cin >> str1;
    cout << "\nYour string is: " << str1 << endl;

    //call function 
    Reverse(str2[81], str1[81]);

    //Output reversed string
    cout << "Your string reversed is: " << str2 << endl;
    cout << "This is a " << "." << endl;


    return 0;
}

void Reverse(char *str2, char *str1)
{
    char* front, *rear;
    int len = strlen(str1);
    char temp;

    front = str1;
    rear = &str1[len - 1];

    while(front < rear)
    {
        temp = *front;
        *front = *rear;
        *rear = temp;
        front++;
        rear --;
    }
}

void IsPalindrome(char cstring)
{

}
3
  • I get an error message that tells me theres no matching function for Reverse Your declaration and definition signatures for Reverse do not match, one has 1 argument another has 2 arguments, what is not clear? Commented Apr 11, 2018 at 2:21
  • void Reverse(char *str2[], char *str1[]); and void Reverse(char *str1[])?? See How to debug small programs and talk to the duck... Really, it helps :) Any reason you are not using string instead of char *? Palindrome of string& s is simply return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin()); (you can do the same with char * using 2 pointers the first set to the beginning and next set to s + strlen(s) - 1) Commented Apr 11, 2018 at 2:26
  • @KillzoneKid Sorry I was messing around before I posted this and forgot to change that back. Just fixed it Commented Apr 11, 2018 at 2:38

2 Answers 2

1

Pointers make life harder, don't use them if you don't need them. As your assignment is to reverse a string, so just use string. string is const char* inside but it's much easier to handle.

Beside your problem with pointers and arrays in your code, you are getting your string from input using std::cin. Remember that this way you can't get strings that have white space one them (you will just get the first word).

Also there is some algorithm that do this kind of tasks for you these days but for educational reasons it's not bad to do it yourself.

Here is what you can do, in the code I mentioned places that you could use ready to use algorithms to do the task for you.

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>

void Reverse(std::string &inputStr);
int main()
{
    // user variables
    std::string str1 = "";

    std::cout << "\nPlease input a string (80 chars max): ";
    std::getline(std::cin, str1);
    std::cout << "\nYour string is: " << str1 << std::endl;

    Reverse(str1);
    //You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
    //std::reverse(str1.begin(), str1.end());

    //Output reversed string
    std::cout << "Your string reversed is: " << str1 << std::endl;
    std::cout << "This is a " << "." << std::endl;

    system("pause");
    return 0;
}

void Reverse(std::string &str)
{
    int n = str.length();
    for(int i = 0; i < n / 2; i++)
    {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;

        //You also can use stl algorithm to do the swap for you like below
        //std::swap(str[i], str[n - i - 1]);
    }

}

NOTE: This example will reverse your original string that user entered, if you want to have the both original and reversed, you need to pass a copy of input to the function. something like this :

std::string str2(str1);
Reverse(str2);
std::cout << "Reversed : " << str2;

EDIT: To match your assignment requirements (have a function with two parameters and single word string) you can easily change the above code like this :

void Reverse(std::string inputStr, std::string &outputStr);
int main()
{
    // user variables
    std::string str1 = "";
    std::string str2 = "";

    std::cout << "\nPlease input a string (80 chars max): ";
    std::cin >>str1;
    std::cout << "\nYour string is: " << str1 << std::endl;

    Reverse(str1, str2);
    //You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
    //std::reverse(str1.begin(), str1.end());

    //Output reversed string
    std::cout << "Your string original is: " << str1 << std::endl;
    std::cout << "Your string reversed is: " << str2 << std::endl;

    system("pause");
    return 0;
}

void Reverse(std::string inputStr, std::string &outputStr)
{
    outputStr = inputStr;
    int n = outputStr.length();
    for(int i = 0; i < n / 2; i++)
    {
        char temp = outputStr[i];
        outputStr[i] = outputStr[n - i - 1];
        outputStr[n - i - 1] = temp;

        //You also can use stl algorithm to do the swap for you like below
        //std::swap(str[i], str[n - i - 1]);
    }   
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to say that I only have to reverse the letters of 1 word. But the main requirements I have is I have to use 2 parameters for the function.
Is there a way to put a character limit of 80 onto a string?
@HollyThorsted There are some approaches that you can use to limit your string size but I don't think you can use them in your assignment, How about checking str1 after user entered the input? you can check your string size and if it's bigger than 80, you can cout that this is not valid string, or you can keep first 80 characters and remover the rest and then do the reversing operation.
0

There is no need of using pointers for reversing a character array to another character array.

void Reverse(char *str2, char *str1)
{
    int len = strlen(str1);    
    for(int n = 0; n< len; n++)
    {
       str2[n] = str1[len-n-1];
    }
}

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.