C++ implementation to determine whether the string is palindromic instance parsing

  • 2020-04-02 02:24:44
  • OfStack

In this paper, the example of C++ is analyzed to determine whether a string is palindrome, through the data structure of the relevant examples, palindrome judgment using filtering space characters, effective characters in order to push the method to achieve this function.

The specific example code is as follows:


#include <iostream>
using namespace std;
#define Max_String_Len 100
#include "SqStack.h"
//Determines whether a string is palindromic
bool ispalindrome(char *in_string)
{
 SqStack <char> s(Max_String_Len);
    char deblankstring[Max_String_Len], c;
 int i = 0;
 //Filter space character
 while(*in_string != '0'){
    if(*in_string != ' ')
 deblankstring[i++] = *in_string;
 in_string++;
  }
  deblankstring[i] = '0';
 //Valid characters are pushed in sequence
 i = 0;
 while(deblankstring[i] != '0')
    s.Push(deblankstring[i++]);
 //Pop-up characters from the stack are compared in sequence
    i = 0;
  while(!s.Empty()){
 c = s.Top();
    s.Pop();
    if(c != deblankstring[i])
      return false;
      i++;
  }
 return true;
}
int main()
{
  char instring[Max_String_Len];
  cout << "input a string:" << endl;
  cin.get(instring, Max_String_Len);
  //cout<<instring;
  if(ispalindrome(instring))
    cout << """ << instring << """ << " is a palindrome." << endl;
  else
    cout << """ << instring << """ << " is not a palindrome." << endl;
  system("pause");
  return 0;
}


Related articles: