Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

String backwards(c++)

+1 vote
asked Sep 29, 2019 by 이민규 (130 points)
edited Sep 30, 2019 by 이민규

#include <iostream>

#include <string>

using namespace std;

class Reverse {

private:

string text; //text에 빈 문자열 준다.

int size;

public:

Reverse();

void reverse_str();

~Reverse() { cout << "Excellent!!" << endl; }

};

Reverse::Reverse() {

string text;

cout << "입력한 문자를 거꾸로 출력하는 프로그램" << endl;

cout << "문자열 입력(exit 입력시 종료) >> " << endl;

getline(cin, text);

}

void Reverse::reverse_str() {

for (;;) {

if (text == "exit")

break;

for (int i = text.length() - 1; i >= 0; --i)

cout << text[i];

cout << endl;

cout << "입력한 문자열의 역순 출력 >>" << endl << text << endl;

}

}

int main()

{

Reverse();

void reverse_str();

}

Take an English sentence and print it backwards

1 Answer

0 votes
answered Oct 1, 2019 by Gaurav Kumar (150 points)
#include <iostream>

#include <string>

using namespace std;

class Reverse {

private:

string text; //text에 빈 문자열 준다.

int size;

public:

Reverse();

void reverse_str();

~Reverse() { cout << "Excellent!!" << endl; }

};

Reverse::Reverse() {

string text;

cout << "입력한 문자를 거꾸로 출력하는 프로그램" << endl;

cout << "문자열 입력(exit 입력시 종료) >> " << endl;

getline(cin, text);

}

void Reverse::reverse_str() {

for (;;) {

if (text == "exit")

break;

for (int i = text.length() - 1; i >= 0; --i)

cout << text[i];

cout << endl;

cout << "입력한 문자열의 역순 출력 >>" << endl << text << endl;

}

}

int main()

{

Reverse();

void reverse_str();

}
commented Oct 3, 2019 by Uzair Anwar (360 points)
you define string text again in  Reverse function so value store in it which you can not access because they are global variable .just remove it and call function obj of class.no need of exit if you want you print a message in if condition

#include <iostream>

#include <string>

using namespace std;

class Reverse {

private:

string text; //text에 빈 문자열 준다.

int size;

public:

Reverse();

void reverse_str();

~Reverse() { cout << "Excellent!!" << endl; }

};

Reverse::Reverse() {

string text;

cout << "입력한 문자를 거꾸로 출력하는 프로그램" << endl;

cout << "문자열 입력(exit 입력시 종료) >> " << endl;

getline(cin, text);

}

void Reverse::reverse_str() {

if (text == "exit")
cout<<"wrong ";

for (int i = text.length() - 1; i >= 0; --i)

cout << text[i];

cout << endl;

cout << "입력한 문자열의 역순 출력 >>" << endl << text << endl;

}
int main()

{
Reverse obj;
Reverse();

obj.reverse_str();

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...