Programming to determine whether a string is a palindrome. Palindrome refers to a sequence of characters with an intermediate character as the datum on both sides of the character exactly the same, such as the character sequence "ACBDEDBCA" is a palindrome.
Algorithm thought: To determine whether a character sequence is a palindrome, is to compare the first character with the last word typeface, the second character is compared with the second-lowest character, and so on, and so on, and the n-i character is compared with the first character. If each comparison is equal, then a palindrome, if a comparison is not equal, it is not a palindrome. Therefore, you can put the sequence of characters into the queue and stack, and then out of the queue and out of the stack and compare the queue characters and the characters are equal, if all the same character sequence is a palindrome, otherwise it is not a palindrome.
Program implementation:
Static voidMain (string[] args) { stringstr = Console.ReadLine ();//Save the characters you enteredstack<Char>stack=Newstack<Char> ();//Create a new stackqueue<Char>queue=Newqueue<Char> ();//Create a new queue for(inti =0; I < Str. Length; i++)//put each character into a queue{stack. Push (Str[i]); Queue. Enqueue (Str[i]); } BOOLIshui =true;//defines a flag for determining whether it is while(Stack. Count >0) { if(Stack. Pop ()! = queue. Dequeue ())//separate the stack out queue to determine whether they are equal{//The stack is starting from the tail out of the queue is starting from scratch so can be reached to detect whether PalindromeIshui =false; Break; }} Console.WriteLine ("whether it is a palindrome string:"+Ishui); Console.readkey (); }
Results:
Simple data structure (quad) stack and simple application of queues