[英]Checking if a word is a palindrome by reversing a stack without using any prewritten functions
#include <iostream>
#include <string>
using namespace std;
#define Max 100000
class Stack {
private:
int top =-1;
char letters[Max];
public:
void setTop(int t) {
top = t;
}
int getTop() {
return top;
}
bool isEmptyStack() {
if (top == -1) {
return true;
}
else{ return false;
}
}
char push(char x,int s) {
if (top != s - 1){
top++;
x = letters[top];
return x;
}
}
char pop() {
if ((isEmptyStack())==false){
cout << "the deleted value is: " << l[top]<<endl;
top--;
return l[top];
}
}
};
void reverse(char letters[], char temp[], int size, Stack stack) {
int i=0;
for (i = 0; i < size; i++) {
stack.push(letters[i],size);
}
i = 0;
cout << temp<<endl;
while (stack.isEmptyStack() == false)
{
letters[-1] = stack.getTop();
stack.pop();
stack.push(letters[i],size);
i++;
}
/* for (int i = 0; i < size; i++) {
cout << temp[i];
}*/
}
int myStringLength(const char* letter)
{
for (int i = 0, c = 0; letter[i] != '\0'; i++, c++) {
if (letter[i] != '\0')
for (; letter[i] != '\0'; i++, c++)
if (letter[i] == '\0') break;
return c;
}
}
int main()
//initializes the main function
{
Stack stack;
string w;
std::cout << "Enter a Word: ";
getline(cin,w);
char* letters = &w[0];
// sets the character text array to set the number of characters equal to the size of the string
//calls the processData function
std::cout << letters<<endl;
int size = myStringLength(letters);
reverse(letters, letters, size, stack);
return 0;//returns the function at 0.
}
I set out to create a program that will check if a word is a palindrome(meaning it is spelled the same normally and if the word is reversed.) I am not yet at that point that is just the final objective.我开始创建一个程序来检查一个单词是否是回文(意味着它的拼写通常相同,如果这个单词被颠倒了。)我还没有到那只是最终目标。 In my code, I have created a stack class because I wanted to feel the satisfaction of getting the same result using my own code.在我的代码中,我创建了一个堆栈 class,因为我想感受使用我自己的代码获得相同结果的满足感。 My problem is the stack is not reversing it is returning some weird characters that I don't have the keys on my keyboard to replicate.我的问题是堆栈没有反转它返回一些奇怪的字符,我的键盘上没有键可以复制。 The desired outcome should be word's reversed characters.期望的结果应该是单词的反转字符。 if the word is food the function should be returning doof .如果这个词是食物function 应该返回doof 。 I have already compared the reversed stack to the original and printed the final statement.我已经将反向堆栈与原始堆栈进行了比较并打印了最终语句。 I fixed the char letters[];我修复了字符字母[];
If you're open to using a simple function instead of a Stack
then you could use the following program since it is much more simple than your Stack
version and hence less-error prone .如果您愿意使用简单的 function 而不是Stack
,那么您可以使用以下程序,因为它比您的Stack
版本简单得多,因此更不容易出错。
#include <iostream>
#include <string>
bool checkIfPalindroom(const std::string &str)
{
for(int i=0;i<(str.size()/2);i++)
{
if (str[i] != str[str.size() - i - 1])
{
return false;//if this if is satisfied even once, return false
}
}
return true;//if the control flow reaches here this will mean that no if was satisfied and hence return true
}
int main()
{
std::string myString = "Somearbitrarystring";
if(checkIfPalindroom(myString))//call the function
{
std::cout<<"The given string: "<<myString <<" is a palindrome"<<std::endl;
}
else
{
std::cout<<"The given string: "<<myString<<" is not a palindrome"<<std::endl;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.