issue to compare char

I need to compare two characters at line 101. But even though they are equal, it does not enter at the right condition. trying ababcbaba when it tries to compare b with b, somehow it does not return as equal. Mainly I need to compare if the left part of the string before the letter c is equal to its right part after c at reverse mode.

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
107
108
109
110
111
112
113
114
115
116
/*
*stack that identify if the string
* before the letter c is the same as
* the one after c but reverse order
* xCy
*/

#include <string.h>
#include <iostream>
#include <stdio.h>
using namespace std;


class noh {
    friend class stack;
private:
    noh* next;
    char dado;
};

class stack {
private:
    noh* topo;
    unsigned int tamanho;
public:
    stack();
    ~stack();
    char pop();
    void push(char d);
    inline bool empty();
    char spy();
};

stack::stack() {
    topo = NULL;
    tamanho = 0;
}

stack::~stack() {
    while(topo != NULL) {
	pop();
    }
    
}

void stack::push(char d) {
    noh* novo = new noh();
    
    novo->dado = d;
    novo->next = topo;
    topo = novo;
    tamanho++;
}

inline bool stack::empty(){
    return(topo == NULL);
}

char stack::pop() {
    if(empty()) {
	return -1;
    } else {
    int removido;
    noh* temp;
    
    removido = topo->dado;
    temp = topo;
    topo = topo->next;
    delete temp;
    tamanho--;
    
    return removido;
    }
}

char stack::spy(){
    return(topo->dado);
}

int main(){
    string word;
    cin >> word;
    
    char* novo = new char[word.size()+1];
    
    strcpy(novo, word.c_str());
    novo[word.size()] = -1;
    
    unsigned int counter = 0;
    
    stack firstPart;
    
    while(novo[counter] != 'c') {
	firstPart.push(novo[counter]);
	counter++;
    }
    counter++;
    bool stop = false;
    while(!stop){
	cout << firstPart.pop() <<' '<< novo[counter]<<endl;
	if (firstPart.pop() == novo[counter]) {
	    counter++;
	} else {
	    stop = true;
	}
    }
    
    if (!stop) {
	cout << "A forma de entrada 'e da forma yCx" << endl;
    } else {
	cout << "A foma de entrada nao 'e da forma yCx" << endl; 
    }
    
    return 0;
}
Note that you are popping two elements in each iteration of the loop. Once on line 100 and again on line 101. If you want to use the same char on both lines you should call pop() only once and store the result in a variable and then you can use it as many times as you want without affecting the stack further.

You also need to think of a way to stop the loop when you have found that the two sides are equal. And how do you handle the situation when the two parts have different length?

When you print the message at the end are you sure you should use the variable stop? Won't stop always be true after the loop has ended?
Last edited on
Thanks! I haven't noticed those problems!
Topic archived. No new replies allowed.