Thread: palindrome - Help needed.

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    8

    palindrome - Help needed.

    i was able to convert the string into case insensitive and remove special characters but now all i end up with is the same output - it is a palindrome.
    please rectify the code and let me know how to fix it.

    Code:
    #include<iostream>      
    //We are creating a string and 2 stacks. Then we push each char of the string into the stack and pop it into an another and compare them.
    using namespace std;
    struct Stack {
      char *arr;
      int size;
      int capacity;
      int top;
    
      Stack(int n) {
        capacity = n;
        top = 0;
        size = 0;
        arr = new char;
      } 
      char pop() {
        if (size == 0) {
          cout << "Stack empty" << endl;
          return '0';
        }
        char ch = arr[0];
        for (int i = 0; i < size - 1; i++) {
          arr[i] = arr[i + 1];
        }
        size--;
        return ch;
      }
      void display() {
        if (size == 0) {
          cout << "Stack empty" << endl;
          return;
        }
        cout << "[";
        for (int i = 0; i < size - 1; i++) {
          cout << arr << ",";
        }
        cout << arr[size - 1] << "]" << endl;
      }
      void push(int data) {
        if (size == capacity) {
          cout << "Stack full" << endl;
          return;
        }
        for (int i = size; i > 0; i--) {
          arr[i] = arr[i - 1];
        }
        arr[0] = data;
        size++;
        return;
      }
    };
    
    int main()
    {
      cout << "Enter letters/words/numbers/phrases :" << endl;
      std::string x;
      std::string temp;
      do {
        getline(cin, x);            //input string
      }
      while (x.empty());            // removing spaces
      x.erase((remove(x.begin(), x.end(), ' ')), x.end());
      std::transform(x.begin(), x.end(), x.begin(),::tolower);  // transform tolower
      for (int i = 0; i < x.size(); ++i) {
        if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) //initializing condition  
        {
          temp = temp + x;          //passing characters that match condition to temp string from initialized string
        }
      }
      cout << temp;
      int n = x.size();
      Stack stack(n / 2);
      int i = 0;
      for (i = 0; i < n / 2; i++) {
        stack.push(x);
      }
      if (n % 2 != 0) {
        i++;
      }
      while (i < x.size()) {
        if (stack.pop() != x) {
          cout << "The word/phrase/number is not a palindrome" << endl;
          break;
        }
        i++;
      }
      if (i == x.size()) {
        cout << "The word/phrase/number is a palindrome" << endl;
      }
    }
    Last edited by Salem; 09-30-2022 at 10:00 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Can you post your code again, but this time using "copy as text" and/or "paste as text".
    I tried to fix your post, but it broke your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    Sure.

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    Code:
    #include <iostream> 
    using namespace std;
    struct Stack{
        char *arr;
        int size;
        int capacity;
        int top;
        Stack ( int n ) {
            capacity = n;
            top = 0;
            size = 0;
            arr = new char;
        }
        char pop () {
            if( size == 0) {
                cout << "Stack empty"<< endl;
                return '0';
            }
            char ch = arr[0];
            for(int i = 0; i < size-1; i++ ){
                arr[i] = arr[i+1];
            }
            size--;
            return ch;
        }
        void display(){
            if(size == 0){
                cout<< "Stack empty" << endl;
                return;
            }
            cout <<"[";
            for ( int i = 0; i < size-1; i++ ) {
                cout << arr[i] <<",";
            }
            cout << arr[size-1] <<"]" << endl;
        }
         void push (int data) {
            if( size == capacity ) {
                cout<<"Stack full"<<endl;
                return;
            }
            for( int i = size; i > 0; i--) {
                arr[i] = arr[i-1];
            }
            arr[0] = data;
            size++;
            return;
        }
    };
    int main(){
        cout << "Enter letters/words/numbers/phrases :" <<endl;
        std :: string x;
        std :: string temp;
        do {
        getline(cin, x); //input string
        }
        while (x.empty () ) ; // removing spaces
        x.erase( (remove(x.begin(),x.end(),' ') ),x.end() );
        std::transform(x.begin(), x.end(), x.begin(), ::tolower); // transform tolower
        for (int i = 0; i < x.size(); ++i)
        {
            if ((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z')) //initializing condition 
            {
                temp = temp + x[i];//passing characters that match condition to temp string from initialized string
            }
        }
        cout << temp;
        int n = x.size();
        Stack stack(n/2);
        int i = 0;
       for(i=0; i < n/2; i++){
            stack.push ( x[i] );
        }
        if(n%2 != 0) {
            i++;
        }
        while (i < x.size() ) {
            if (stack.pop() != x[i]) {
                cout<< "The word/phrase/number is not a palindrome" << endl;
                break;
            }
            i++;
        }
        if ( i == x.size() ) {
            cout<< "The word/phrase/number is a palindrome" << endl;
        }
    }
    Last edited by Salem; 10-01-2022 at 10:00 AM. Reason: Better, at least it was only bold

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suppose that was an improvement, at least it was easy to cleanup.

    > arr = new char;
    You don't allocate the size you requested.

    Your assignment clearly states that you need to use TWO stacks, but you only have one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    i dont get it. when i cout << temp; i get the string with special characters removed and all lower case. maybe im doing something wrong with comparing them ?

    Actual Output:
    Enter letters/words/numbers/phrases :
    Madam I’m Adam
    madamimadam The word/phrase/number is not a palindrome

    Expected output:
    Enter letters/words/numbers/phrases :
    Madam I’m Adam
    madamimadam The word/phrase/number is a palindrome

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    When you do the test for a palindrome you need to do it with temp, not the original x that still has the extra characters in it. Also, you need to allocate n chars in Stack, not just 1.

    Also, your algorithm for the stack is inefficient. There's no need to move the contents of the array back and forth. Instead of adding new elements to the beginning (element 0), add them to the end instead!
    Last edited by john.c; 10-02-2022 at 11:01 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Sep 2022
    Posts
    8
    I corrected my code and just need help with a FOR statement.
    i would like the string to include numbers as well.
    At the moment it only accepts upper and lower case characters and excludes the rest.
    Please help!! My deadline is due ( T T )
    Code:
     
    for (int i = 0; i < S.size(); i++) {   //Remove Special Character.
            if (S[i] < 'A' || S[i] > 'Z' &&
                S[i] < 'a' || S[i] > 'z'){         
                   S.erase(i, 1);
                   i--;
                }
            }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (S[i] < 'A' || S[i] > 'Z' &&
    You've swapped over the && and || compared to post #4 (and broken what you had)

    All you needed was another
    || ( x[i] >= '0' && x[i] <= '9' )

    Or maybe
    Null-terminated byte strings - cppreference.com
    if ( isupper(x[i]) || islower(x[i]) || isdigit(x[i]) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome C++
    By GrafixFairy in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2014, 08:53 AM
  2. Palindrome
    By jturner38 in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2010, 09:42 PM
  3. Palindrome
    By jturner38 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2010, 06:51 AM
  4. Palindrome in C
    By DaniiChris in forum C Programming
    Replies: 13
    Last Post: 07-24-2008, 11:32 PM
  5. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM

Tags for this Thread