palindrome using stack

Write your question here.
in this code, before the push error made it. code can operate but there is problem in while code that push to stack
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
  int main()
{
	Stack s1, s2;

	
	
	string buf;
	char buf2;
	int flag = 0;	
	int count = 0;	
	while (true)
	{
		cout << "Enter a word or a sentence(If you want to exit,insert Ctrl+Z).\n";
		cin >> buf;
		int l = buf.size();	
			
		if (cin.eof())	//ctrl+z 
		{
			break;
		}
		

		s1.Initialize(buf.size() + 1);	
		s2.Initialize(buf.size() + 1);	
		
		const char * bufpt = buf.c_str();
		char * buf3 = new char[buf.size() + 1];
		
		strcpy(buf3, buf.c_str());
		
		
		//problem is here
		
		while (count <= (l - 1))
		{
			char c = buf3[count];
			s1.Push(c);//push the string to s1
			count++;
		}
		count = 0;
		
		
		
		for (int i=0;i<l/2;i++)
		{
			s1.Pop (buf2);
			s2.Push(buf2);

			

			if (s1.mbuffer[i] != s2.mbuffer[i])
			{
				flag = 1;
				break;
			}
			
			
		}

		if (flag == 1)
		{
			cout << "No,it is not a palindrome.\n";
			
		}
		else
		{
			cout << "Yes,it is a palindrome.\n";
		}
		
		delete[] buf3;
		s1.RemoveAll();
		s2.RemoveAll();
	}

	
	return 0;
}
ello khpark8511,

Welcome to the forum.

You have left out the beginning of the file that you posted. I do not know what header files you have used.

"Stack" looks to be a type, but is it a class or struct? Without the files that cover that it makes it hard to test the program.

Is this an exercise in pointers? Because lines 26 - 29 could be replaced with buf3 = buf; provided "buf3" is defined as a std::string.

Hope that helps,

Andy
i edited my code ..
this is main function

#include "stack.h"
#include <iostream>
#include <string>
#include <cstring>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;

int main()
{
Stack s1, s2;

int count = 0;
int size;
int flag = 0;
string word;
char x, c;


while (true)
{
cout << "Enter a word or a sentence(If you want to exit,insert Ctrl+Z).\n";
getline(cin, word);
size = word.size() + 1;

if (cin.eof())
{
break;
}




char *arr = new char[size];
strcpy(arr, word.c_str());

s1.Initialize(size);
s2.Initialize(size);

while (count <= (size - 1))
{
c = arr[count];
s1.Push(c);
count++;
}
count = 0;

while (count < (size-1)/2)
{
s1.Pop(x);
s2.Push(x);

if(s2.mbuffer[count] != s1.mbuffer[count])
{

flag = 1;
break;
}

count++;
}

if (flag == 1)
{
cout << "No,it is not a palindrome.\n";
}
else
{
cout << "Yes,it is a palindrome.\n";

}

delete[] arr;
s1.RemoveAll();
s2.RemoveAll();


}

return 0;
}


now i resolve that problem and program is operating but the answer is wrong...

this is header file
#ifndef STACK_H
#define STACK_H

#pragma once
//Stack.h
#include<iostream>
#include<string>
using namespace std;

class Stack {
public:
int msize;
int mtop;
char *mbuffer;

void Initialize(int size);
void RemoveAll();
bool Push(char value);
bool Pop(char& value);
};

#endif // ! STACK_H


and this is palindrome.cpp
#include "stack.h"
#include <iostream>
#include <string>

void Stack::Initialize(int size)
{
mbuffer = new char[size];
msize = size;
mtop = -1;
memset(mbuffer, 0, sizeof(char)*msize);
}

void Stack::RemoveAll()
{
msize = 0;
mtop = -1;
delete[] mbuffer;
mbuffer = NULL;
}

bool Stack::Push(char value)
{
if (mtop > msize - 1)
{
return false;
}
mbuffer[++mtop]=value;
return true;
}

bool Stack::Pop(char& value)
{
if (mtop < 0)
{
return false;
}
value = mbuffer[mtop--];
return true;

}



Hello khpark8511,

Finally figured it out. in line 14 you are using "cin >> buf" to get the input. this works fine for a single word, but not for a string with spaces. I changed "cin" to std::getline(std::cin, buf); and it works fine now.

I do not understand why you are taking the hard way do do something simple, but it does work.

What you kneed to understand is that "cin" will extract what you type in up to either the first white space or '\n' whichever comes first where "getline" will extract everything up to and including the '\n'.

Hope that helps,

Andy

Edit:
Last edited on
really appreciate it ! after i asked this problem, i edited code in other way . really thank you! i think i don't know well about input .. thankyou somuch
Topic archived. No new replies allowed.