Strange problem while reversing string

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
#include<iostream>
#include<cstring>
using namespace std;
void push(char *str2,int *max,int *top,char *item)
{
	if(*top==*max-1)
		cout<<endl<<"Stack is full";
	else
		{
			*top=*top+1;
			str2[*top]=*item;
		}
}

void pop(char *str2,int *top,char *item)
{
	if(*top==-1)
		cout<<endl<<"Stack is empty";
	else
		{
			*item=str2[*top];
			*top=*top-1;
		}
}

void display(char *str2, int len)
{
	cout<<endl;
	for(int i=0;i<len;i++)
		cout<<str2[i];
}
int main()
{
	int max=25,top=-1,i,len;
	char str[25],str2[25],item;

	cout<<"Enter a string :  ";
	cin>>str;
	len=strlen(str);

	for(i=0;i<len;i++)
	{
		item=str[i];
		push(str2,&max,&top,&item);
	}
	for(i=0;i<len;i++)
	{
		pop(str2,&top,&item);
		str2[i]=item;
	}
	cout<<endl;
	display(str2,len);
	return 0;
}

I need help as I'm a beginner
The above code displays sulus if cplus was given as input,
and 654456 if 123456 was given as input,
please help me.
You are modifying same array used for your stack.
Thank you very much MiiNiPaa, you saved my hours!!!!!
Topic archived. No new replies allowed.