-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractical_25(C++).cpp
107 lines (82 loc) · 2.32 KB
/
Practical_25(C++).cpp
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
/*
A palindrome is a string of character that‘s the same forward and backward. Typically, punctuation, capitalization,
and spaces are ignored. For example, “Poor Dan is in a droop” is a palindrome, as can be seen by examining the characters
“poor danisina droop” and observing that they are the same forward and backward. One way to check for a palindrome is to reverse
the characters in the string and then compare with them the original-in a palindrome, the sequence will be identical. Write C++ program
with functions-
a) To print original string followed by reversed string using stack
b) To check whether given string is palindrome or not
*/
#include<iostream>
#include<string.h>
#define max 50
using namespace std;
class STACK {
private:
char a[max];
int top;
public:
STACK() {
top = -1;
}
void push(char);
void reverse();
void convert(char[]);
void palindrome();
};
void STACK::push(char c) {
top++;
a[top] = c;
a[top + 1] = '\0';
cout << endl << c << " is pushed on stack ...";
}
void STACK::reverse() {
char str[max];
cout << "\n\nReverse string is : ";
for (int i = top, j = 0; i >= 0; i--, j++) {
cout << a[i];
str[j] = a[i];
}
cout << endl;
}
void STACK::convert(char str[]) {
int j, k, len = strlen(str);
for (j = 0, k = 0; j < len; j++) {
if (((int)str[j] >= 97 && (int)str[j] <= 122) || ((int)str[j] >= 65 && (int)str[j] <= 90)) {
if ((int)str[j] <= 90) {
str[k] = (char)((int)str[j] + 32);
} else {
str[k] = str[j];
}
k++;
}
}
str[k] = '\0';
cout << endl << "Converted String : " << str << "\n";
}
void STACK::palindrome() {
char str[max];
int i, j;
for (i = top, j = 0; i >= 0; i--, j++) {
str[j] = a[i];
}
str[j] = '\0';
if (strcmp(str, a) == 0)
cout << "\n\nString is palindrome...";
else
cout << "\n\nString is not palindrome...";
}
int main() {
STACK stack;
char str[max];
int i = 0;
cout << "\nEnter string to be reversed and checked if it's palindrome or not: \n\n";
cin.getline(str, 50);
stack.convert(str);
while (str[i] != '\0') {
stack.push(str[i]);
i++;
}
stack.palindrome();
stack.reverse();
}