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

Pages: 12
Yes got that! I have one more question!
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 #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[]);
  void reverse(string inputString);
 
  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
    bool running = true;
 
  do{
    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;
 
    switch(choice)
    {
      case 1:(choice == 1);{
              string inputString;
 
              cout << "\n\nPlease enter a word or phrase: ";
              cin.ignore();
              getline(cin, inputString);
 
              reverse(inputString);
              break;
             }
 
       case 2:(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;
              break;
              }
 
       default:{
               cout << "Thank you and have a good day!" << endl;
               running = false;
               break;
                }
    }
 
  }while(running);
 
       ///here is wher the reverse string part should go.
 
  return 0;
  }
 
 
  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);
    }
 }

 void reverse(string inputString){

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

 }


So this code compiles and runs great until I select "2" and input "mom" and it tells me its a palindrome just fine. Now if I enter "mom dad" the program freaks out and just keeps repeating itself forever until I force stop the function. Now if I use the return 0; that is commented out in the palindrome case #2 it stops when it is a palindrome with 2 or more words. But it stops the program and does not do the do while loop. Why does this program freak out?
Line 47: cin >> inputString; can get only one word. In your case it leaves " dad" in input buffer. Next it tries to get int choice, finds 'd' symbol, which isn't a digit and cannot be converted to int, sets failbit, and starts over, finds 'd' char...
Last edited on
okay, have any idea how I can fix this? please.
Lines 36, 37:
1
2
cin.ignore();
getline(cin, inputString);

You did use this inctead of cin >> inputString on purpose, did you?
@bruntmjust


In the first post of your thread you wrote:

"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."
And as it follows you meant a character array when you were saying about a string.

So the code you have now does not satisfies your first requirements. First of all the function is not recursive and secondly it does not use a character array.
Last edited on
yes
so what would I have to change?
If you don't have a struct like the one I posted, all you need is the length of the word and give that as a parameter to the function

1
2
3
4
5
6
7
8
9
10
void Print(string myword, int length)
{
  cout << myword[length-1];
  if (length == 0)
  {
    cout <<endl;
    return;
  }
  Print(myword, length-1);
}
I kind of see. With my code above what would I have to change that with? Because I have different representations of the words and such.
line 14:
void reverse(string , int);

line 38:
cout << "String reversed ";

line 39:
reverse(inputString, inputString.size());

line 101-110:
1
2
3
4
5
6
7
8
9
10
void reverse(string inputString, int length)
{
  cout << inputString[length-1];
  if (length == 0)
  {
    cout <<endl;
    return;
  }
  reverse(inputString, length-1);
}



Also notice line 21 you have:
char inputString[100];//initialize size of array

Do you need it?
yes i do need that and when I tried this it gave me this repeating:
YPuTTYollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1oll1ÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿÿ1ollehÿÿÿ
I think I may have found my problem
Do you know how I could fix my palindrome function?
I thought your palindrome function was working? Did you look at the one in the link I posted?
it works for single words. When I put in two words such as "mom dad" it begins repeating this:

Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!

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)


Please enter a word or phrase: You have entered a palindrome!!!
Yea, that's why I hate using cin, try using cin.getline(inputString, 100) or scanf or fgets.

Look at the isPal function in the link I posted, that should help you along to solving this. I gtg to bed

gl
Okay that did fix it! Now one last problem.
When I type "mom dad" it says it is not a palindrome.
How can I fix it so it is correct?

may answer when you have time. thank you.
just kidding I got it, thank you again.
Topic archived. No new replies allowed.
Pages: 12