Problem in the code

Write a program to create palindrome words by using the string entered by users. Your program should have 2 strings: the first string is used to store input text from the user and the second string is to store the same input text from the user in reverse direction. Display the palindrome words at the end of your program.

Sample of input: ABCD

Sample of output: ABCDDCBA


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()

{
	char i;
    string str1[i], str2[i];
    cout << "please Enter the text  " << ": "<<"\n";
    cin >> str1[i];


for (int i = 0; i < 10; i++)

	cout << str1[i] << endl;


cout << "\n";
	
for (int i = 9; i >= 0; i--)
	
        cout << str2[i] << endl;

return 0;
}
Are you supposed to use C-strings or std::strings?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
//#include <string> //uncomment if using std::string
using namespace std;

int main() {
    char str1[255], str2[255]; //comment this if using std::string
    //string str1, str2; //uncomment if using std::string
    cout << "please Enter the text  " << ": "<<"\n";
    cin >> str1;
    //comment all below if using std::string
    int endIndex = 0;
    for(; str1[endIndex] != '\0' && str1 < 255; endIndex ++); //find the end of the input
    for(int i = endIndex, int i2 = 0; i >= 0; i --, i2 ++) {
        str2[i2] = str1[i];
    }
    cout << str1 << str2 << endl;
    //uncomment all below if using std::string
    /*
    for(int i = str1.length() - 1; i >= 0; i --) {
        str2 += str1[i];
    }
    cout << str1 << str2 << endl;
    */
    return 0;
}
I should use a string but I am facing a problem with applying it in the program that you gave me


1
2
3
4
   int endIndex = 0;
    for(; str1[endIndex] != '\0' && str1 < 255; endIndex ++); //find the end of the input
    for(int i = endIndex, int i2 = 0; i >= 0; i --, i2 ++) {
        str2[i2] = str1[i];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main() 
{
    string str1; 
    cout << "please Enter the text  " << ": "<<"\n";
   getline(cin,str1);
   str1= string(str1.rbegin(),str1.rend());
   
   cout << " the reverse direction of the text "<< ": " <<"\n" << str1 << endl;
   cout << "the palindrome words  "<< ": " <<"\n";
    cout <<  string(str1.rbegin(),str1.rend()) << str1;
    return 0;
}
Topic archived. No new replies allowed.