Change from using STL to not using stl

i wrote a palindrome checker using a stack and a queue but i have to change it to where its using Non-Stl code. My issues are where i am removing punctuation and spaces. i have searched around but cant find any reliable way of doing this. Is there a way i can do this by just not adding those characters to the queue and stack at all and not have to worry about removing them before hand?

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

#include <iostream>
#include "stack.h"
#include "queue.h"
#include <cctype>
using namespace std;

void RemovePunct(string text, string &result);
void RemoveSpaces(string text, string &result);
void toUpperCase(string s);

int main()
{
    bool isPalindrome = false;
    Stack<char> palindromeStack;
    Queue<char> palindromeQueue;
    string palindrome, palindromePunctDel, palindromeSpacesDel;
    cout << "Welcome to the Palindrome Checker\nWhat would you like to check?\n";
    getline(cin, palindrome);
    RemovePunct(palindrome, palindromePunctDel);
    RemoveSpaces(palindromePunctDel, palindromeSpacesDel);
    toUpperCase(palindromeSpacesDel);
    
    for (int x; x < palindromeSpacesDel.length(); x++)
    {
        palindromeStack.push(palindromeSpacesDel[x]);
        palindromeQueue.enqueue(palindromeSpacesDel[x]);
        
    }
    while (!palindromeStack.isEmpty())
    {
        char x;
        char y;
        palindromeStack.getTop(x);
        palindromeStack.pop();
        palindromeQueue.getFront(y);
        palindromeQueue.dequeue();
        
        if (x == y) {
            isPalindrome = true;
        }
        else
        {
            isPalindrome = false;
        }
    }
    if (isPalindrome == true)
    {
        cout << palindrome << " is a palindrome"<< endl;
    }
    else
    {
        cout << palindrome << " is not a palindrome"<< endl;
    }
    return 0;
    
    }


void toUpperCase(string s)
{
    for (int i = 0; i < s.size(); ++i)
    {
        s[i] = toupper(s[i]);
    }
}

void RemovePunct(string text, string &result)
{
    remove_copy_if(text.begin(), text.end(),back_inserter(result), ptr_fun<int, int>(ispunct));
}

void RemoveSpaces(string text, string &result)
{
    remove_copy_if(text.begin(), text.end(),back_inserter(result), ptr_fun<int, int>(isspace));
}

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
//*****************************************************************************
//    	File:					stack.h

using namespace std;
#ifndef STACK_H
#define STACK_H

template<class DT>
struct Node
{
	DT info;
	Node<DT>* next;
};

template <class DT>
class Stack
{
private:
    Node<DT>* top;// ponter to top of stack
public:
    
    Stack();
    ~Stack();   // destructor
    bool isEmpty();
    void push(DT&);
    bool pop();
    bool getTop(DT&);
    
    
};
#endif

template<class DT>
Stack<DT>::Stack()
{
    top = NULL;
}

template<class DT>
Stack<DT>::~Stack()
{
    while (top != NULL)
        pop();
}

template<class DT>
bool Stack<DT>::isEmpty()
{
    return(top==NULL);
}

template<class DT>
void Stack<DT>::push(DT& item)
{
    Node<DT>* ptr = new Node<DT>;
    ptr->info = item;
    ptr->next=top;
    top = ptr;
}

template<class DT>
bool Stack<DT>::getTop(DT& item)
{
    if(isEmpty())
    {
        return false;
    }
    item = top->info;
    return true;
}

template<class DT>
bool Stack<DT>::pop()
{
    if(isEmpty())
        return false; //Because the stack's empty
    Node<DT>* ptr = new Node<DT>;
    ptr = top;
    top = top->next;
    delete ptr;
    return true;
}



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

//    	File:					queue.h


#ifndef QUEUE_H
#define QUEUE_H

using namespace std;

template<class DT>
struct node{
    DT info;
    node<DT>* next;
};
template<class DT>
class Queue
{
    private:
        node<DT> *rear;
        node<DT> *front;
    public:
        Queue();
        void enqueue(DT&);
        void dequeue();
        void display();
        void getFront(DT&);
};
template<class DT>

Queue<DT>::Queue()
{
    rear = NULL;
    front = NULL;
}

template<class DT>
void Queue<DT>::getFront(DT& item)
{
    item = front->info;
}

template<class DT>
void Queue<DT>::dequeue()
{
    node<DT> *temp = new node<DT>;
    
    if(front == NULL)
    {
        cout<<"\nQueue is Emtpty\n";
    }
    else
    {
        temp = front;
        front = front->next;
        delete temp;
    }
}
template<class DT>
void Queue<DT>::display()
{
    node<DT> *p = new node<DT>;
    p = front;
    while(p!=NULL)
    {
    cout<<p->info;
    p = p->next;
    }
}

template<class DT>
void Queue<DT>::enqueue(DT& data)
{
    node<DT> *temp = new node<DT>;
    temp->info = data;
    temp->next = NULL;
    if(front == NULL)
    {
        front = temp;
    }
    else
    {
        rear->next = temp;
    }
    rear = temp;
}
#endif
Last edited on
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
#include <iostream>
#include <string>
#include <cctype>

// remove white space, punctuation, convert all characters to uppercase
std::string sanitise( std::string str )
{
    std::string sanitised ;
    for( char c : str ) if( std::isalnum(c) ) sanitised += std::toupper(c) ;
    return sanitised ;
}

bool is_palindrome( std::string str )
{
    if( !str.empty() )
    {
        str = sanitise(str) ;

        const char* begin = str.c_str() ;
        const char* mid = begin + str.size() / 2 ;
        const char* end = begin + str.size() ;
        while(  begin < mid  )
        {
            --end ;
            if( *begin != *end ) return false ;
            ++begin ;
        }
    }
    return true ; // empty string is a palindrome
}
i was able to fix it by adding this code so it only pushes the non space and punctuation.
1
2
3
4
5
6
7
8
9
10
11
12
13
  
  for (int x; x < palindrome.length(); x++)
    {
        if (isspace(palindrome[x])|| ispunct(palindrome[x]))
        {
            spaceAndPunctCount++;
        }
        else
        {
            palindromeStack.push(palindrome[x]);
            palindromeQueue.enqueue(palindrome[x]);
        }
    }


Thank you JLBorges for the quick reply but im required to use the stack and queue but i really like your sanitised function makes it quick and easy.
Topic archived. No new replies allowed.