Reversing a string help!!!!(it has to be something small)

Pages: 12
So I have created a palindrome function that lets the user input a word or sentence and tells them weather it is a palindrome or not. Now I have added an option to let the user revers the word or sentence. The problem I am having is creating a new recursive function that I will call reverse_string() that takes a string of characters and reverses it. After reversing the string I then want to print the new reversed string to the user.

Here is my code:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  #include<iostream>
  #include<cmath>
  #include<cstring>
  #include<string>
 
  using namespace std;
  using std::endl;
  using std::cout;
  using std::cin;
 
  bool Palindrome(int first, int last, char inputString[]);
  char revers_string();
 
  int main()
  {
    int choice;
    int first = 0;// first index for array
    int last = 0;// last index for array
    char inputString[100];//initialize size of array
 
    cout << " " << endl;
    cout << "Do you want to reverse a string, check if it a palindrome, or quit?" << endl;
    cout << "(Press 1 for reverse, 2 for palindrome, and anything else to quit)" << endl;
    cin >> choice;
 
    if (choice == 2){
    cout << "\n\nPlease enter a word or phrase: ";
 
    cin >> inputString;//receive input from user
    last = strlen(inputString);//store input into character array
 
      if (Palindrome(first,last - 1,inputString)== true)
         cout << "You have entered a palindrome!!!";
 
      else
         cout << "This is not a palindrome.";
         cout << " " << endl;
 
         return 0;
    }
 
    else(choice == 1);
    {
    cout << "\n\nPlease enter a word or phrase: ";
 
 
 
    //here is wher the reverse string part should go.
 
 
 
    }
  }
 
  bool Palindrome(int first, int last, char *inputString){
 
    //if the next first index is greater than the next last index
    //program concludes that all the elements in array have been compared
    //and therefore input should be a palindrome retuned as true.
    if (first > last)
       return true;
 
    //if next first element and next last element are not the same
    //therefore word is not a palindrome returned as false
    if (inputString[first]!= inputString[last])
       return false;
 
    //if next first element and next last element are same
    //call Palindrome function again, increment first element by one
    //decrement last element by one to compare if those two elements
    //in array are equal.Continue this process until next first element
    //and next element are not equal.
    else if (inputString[first] == inputString[last])
    {
       return Palindrome((first+1),(last-1),inputString);
    }
 
  }
 
 


Can someone please help me out. My program is also suppose to quite if the user inputs something other than 1 or 2 and it keeps going if I put in 3 and so on. Any help with be appreciated, thank you.
Last edited on
This kind of helps but I am not sure how I should use this in my code. How would I do the same kind of steps but with my code? I have tried something like that but it does not work. Not sure if I should use a void type function or not.
1
2
3
4
5
6
void reverse_string(char* str)
{
    //There you should find the end of string and swap nth and (length - n)
    //symbols in string in loop until you swap half of the string.
    //Look at the possible implementation in reverse algorithm for example
}
Okay! so I have this:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
  #include<cmath>
  #include<cstring>
  #include<string>
 
  using namespace std;
  using std::endl;
  using std::cout;
  using std::cin;
 
  bool Palindrome(int first, int last, char inputString[]);
  char revers_string();
 
  int main()
  {
    int choice;
    int first = 0;// first index for array
    int last = 0;// last index for array
    char inputString[100];//initialize size of array
 
    cout << " " << endl;
    cout << "Do you want to reverse a string, check if it a palindrome, or quit?" << endl;
    cout << "(Press 1 for reverse, 2 for palindrome, and anything else to quit)" << endl;
    cin >> choice;
 
    if (choice == 2){
    cout << "\n\nPlease enter a word or phrase: ";
 
    cin >> inputString;//receive input from user
    last = strlen(inputString);//store input into character array
 
      if (Palindrome(first,last - 1,inputString)== true)
         cout << "You have entered a palindrome!!!";
 
      else
         cout << "This is not a palindrome.";
         cout << " " << endl;
 
         return 0;
    }
 
    else(choice == 1);
    {
     string inputString;
    
     cout << "\n\nPlease enter a word or phrase: ";
     cin >> inputString;

     string reverse_string(inputString.rbegin(), inputString.rend());
     cout << "\nReverse string is : " << reverse_string << endl;
 
 
    //here is where the reverse string part should go.
 
 
 
    }
  }
 
  bool Palindrome(int first, int last, char *inputString){
 
    //if the next first index is greater than the next last index
    //program concludes that all the elements in array have been compared
    //and therefore input should be a palindrome retuned as true.
    if (first > last)
       return true;
 
    //if next first element and next last element are not the same
    //therefore word is not a palindrome returned as false
    if (inputString[first]!= inputString[last])
       return false;
 
    //if next first element and next last element are same
    //call Palindrome function again, increment first element by one
    //decrement last element by one to compare if those two elements
    //in array are equal.Continue this process until next first element
    //and next element are not equal.
    else if (inputString[first] == inputString[last])
    {
       return Palindrome((first+1),(last-1),inputString);
    }
 
  }
 


Now when I do this it works for when I put "Hello" it outputs "olleH" but when I put "Hello World" it only returns the "olleH" part agin. How do I fix this so it prints the whole phrase backwards?
Anyone have any ideas?
cin >> inputString;
operator>> will get symbols until it encounters an whitespace character.
You can use getline() to take whole line. And read about cin.ignore() to counter unwanted consequences of mixing >> and getline()
When I try to use getline it does not work
I am sorry but that does not help me.

How would I get this to work???

1
2
3
4
5
6
7
string inputString;
    
     cout << "\n\nPlease enter a word or phrase: ";
     cin >> inputString;

     string reverse_string(inputString.rbegin(), inputString.rend());
     cout << "\nReverse string is : " << reverse_string << endl;


It does one work just fine but once there is a space it doesn't read the word after that space.
and I have been reading that getline works some how but I can not seem to figure out how to work it with my code. please someone help.
1
2
3
4
5
6
7
string inputString;
    
     cout << "\n\nPlease enter a word or phrase: ";
    getline(cin, inputString);

     string reverse_string(inputString.rbegin(), inputString.rend());
     cout << "\nReverse string is : " << reverse_string << endl;
Last edited on
see the problem is when I do that it does not letter the user enter anything.
it does this:
"Do you want to reverse a string, check if it a palindrome, or quit?
(Press 1 for reverse, 2 for palindrome, and anything else to quit)
1


Please enter a word or phrase:
Reverse string is :"

It just jumps straight to the end of the progrom without any input.
why???
and if I try this:

1
2
3
4
5
6
7
8
string sentence;
    
     cout << "\n\nPlease enter a word or phrase: ";
    cin >> sentence;
     getline(cin, sentence);

     string reverse_string(sentence.rbegin(), sentence.rend());
     cout << "\nReverse string is : " << reverse_string << endl;


if I input "Hello world"

it only reverses world to dlrow but I want it to do this to both words.
how?
Okay so I figured that out with the cin.ignore() funciton.

So I only have 2 confusions in this prgram.

1) How do I stop the program is the user inputs 3 or higher when the first question is asked?
2) How do I get a do while loop working in this program?

I have tried a bit of both and have had no luck.

Thank you.
1) How do I stop the program is the user inputs 3 or higher when the first question is asked?
1
2
3
4
if (choice > 2) {
    std::cout << "invalid choice" << std::endl;
    return 0;
}


2) How do I get a do while loop working in this program?
Where do you want it ant what purpos should it have?
Use a switch within a do-while loop?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

int main()
{
	int choice = 0;

	bool running = true; // bool for the do-while loop.

	do
	{
		std::cout << "Please enter a number: ";
		std::cin >> choice;

		switch( choice )
		{
			case 1:
				std::cout << "You entered 1.\n";
				// Function call to option 1. Move your code to this function.
				break;

			case 2:
				std::cout << "You entered 2.\n";
				// Function call to option 2.
				break;

			case 3:
				std::cout << "You entered 3. Now exiting...\n";
				running = false; // Exit condition.
				break;

			default: // Anything other than the 3 above case statements.
				std::cout << "You entered an invalid number. Exiting...\n";
				running = false; // Exit condition.
				break;
		}
	}while( running ); // Exit condition.
	
	return 0;
}


For line 18, I would move all my code for this option in to a function. I've always been taught to keep main() as small as possible and use functions.
Last edited on
Thank you so much!!!! I am hitting myself in the head for not thinking of case statements ha
and yes I will be moving code into functions.
Okay so now that things are looking good I am going to put my reverse string code into a function: void reverse(string inputString);

Now this may be a dumb question but how exactly should I do this?
Inlcuding calling the function in main under case 1:
if you are using std::string, you should either return reversed string, or pass inputString by reference:
1
2
3
string reverse(string inputString)
//or
void reverse(string &inputString)
Pages: 12