1 // Student: Pat Moss 2 // Instructor: Dave Hammer 3 // CIS162AD C# 5832 - Project 02 - Palindrome Tester 4 5 // Write a C# program that asks the user for a phrase. 6 // Put the phrase into an array of characters and check 7 // if it is a palindrome. You will have to remove special 8 // (nonalphabetic) characters, storing only alphabetic 9 // characters to a second character array. You also must 10 // convert the characters to all uppercase or all lowercase 11 // as you store them. The first palindrome might become: 12 // MADAMIMADAM 13 14 // Using string functions or one or more "for" or "while" 15 // loops, you now can test the phrase to determine whether 16 // it is a palindrome. Make the screen input and output 17 // attractive and user friendly. Don't forget to break the 18 // program into functions to do the separate tasks. Sample 19 // output should look like this: 20 // "Madam, I'm Adam" is a palindrome. 21 22 using System; 23 using System.Drawing; 24 using System.Collections; 25 using System.ComponentModel; 26 using System.Windows.Forms; 27 using System.Data; 28 29 namespace palindrome 30 { 31 public class palindromeform : System.Windows.Forms.Form 32 { 33 private System.Windows.Forms.Label lblHeader1; 34 private System.Windows.Forms.TextBox txtInputString; 35 private System.Windows.Forms.Label lblPalindromeStatus; 36 private System.Windows.Forms.Button btnCheckPhrase; 37 private System.Windows.Forms.Button btnExit; 38 39 private System.ComponentModel.Container components = null; 40 41 public palindromeform() 42 { 43 InitializeComponent(); 44 } 45 46 protected override void Dispose( bool disposing ) 47 { 48 if( disposing ) 49 { 50 if (components != null) 51 { 52 components.Dispose(); 53 } 54 } 55 base.Dispose( disposing ); 56 } 57 58 #region Windows Form Designer generated code 59 /// 60 /// Required method for Designer support - do not modify 61 /// the contents of this method with the code editor. 62 /// 63 private void InitializeComponent() 64 { 65 this.lblHeader1 = new System.Windows.Forms.Label(); 66 this.txtInputString = new System.Windows.Forms.TextBox(); 67 this.lblPalindromeStatus = new System.Windows.Forms.Label(); 68 this.btnCheckPhrase = new System.Windows.Forms.Button(); 69 this.btnExit = new System.Windows.Forms.Button(); 70 this.SuspendLayout(); 71 // 72 // lblHeader1 73 // 74 this.lblHeader1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 75 this.lblHeader1.Location = new System.Drawing.Point(24, 8); 76 this.lblHeader1.Name = "lblHeader1"; 77 this.lblHeader1.Size = new System.Drawing.Size(216, 32); 78 this.lblHeader1.TabIndex = 0; 79 this.lblHeader1.Text = "Enter a phrase to test:"; 80 // 81 // txtInputString 82 // 83 this.txtInputString.Location = new System.Drawing.Point(24, 48); 84 this.txtInputString.Name = "txtInputString"; 85 this.txtInputString.Size = new System.Drawing.Size(272, 20); 86 this.txtInputString.TabIndex = 1; 87 this.txtInputString.Text = ""; 88 // 89 // lblPalindromeStatus 90 // 91 this.lblPalindromeStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 92 this.lblPalindromeStatus.Location = new System.Drawing.Point(24, 80); 93 this.lblPalindromeStatus.Name = "lblPalindromeStatus"; 94 this.lblPalindromeStatus.Size = new System.Drawing.Size(336, 32); 95 this.lblPalindromeStatus.TabIndex = 2; 96 // 97 // btnCheckPhrase 98 // 99 this.btnCheckPhrase.Location = new System.Drawing.Point(56, 128); 100 this.btnCheckPhrase.Name = "btnCheckPhrase"; 101 this.btnCheckPhrase.Size = new System.Drawing.Size(104, 24); 102 this.btnCheckPhrase.TabIndex = 3; 103 this.btnCheckPhrase.Text = "Check the Phrase"; 104 this.btnCheckPhrase.Click += new System.EventHandler(this.btnCheckPhrase_Click); 105 // 106 // btnExit 107 // 108 this.btnExit.Location = new System.Drawing.Point(208, 128); 109 this.btnExit.Name = "btnExit"; 110 this.btnExit.Size = new System.Drawing.Size(56, 24); 111 this.btnExit.TabIndex = 4; 112 this.btnExit.Text = "Exit"; 113 this.btnExit.Click += new System.EventHandler(this.btnExit_Click); 114 // 115 // palindromeform 116 // 117 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 118 this.ClientSize = new System.Drawing.Size(384, 174); 119 this.Controls.Add(this.btnExit); 120 this.Controls.Add(this.btnCheckPhrase); 121 this.Controls.Add(this.lblPalindromeStatus); 122 this.Controls.Add(this.txtInputString); 123 this.Controls.Add(this.lblHeader1); 124 this.Name = "palindromeform"; 125 this.Text = "Project 02 - Palindrome Tester - by Pat Moss"; 126 this.ResumeLayout(false); 127 128 } 129 #endregion 130 131 static void Main() 132 { 133 Application.Run(new palindromeform()); 134 } 135 136 // check the user input string to determine if it is a palindrome 137 private void btnCheckPhrase_Click(object sender, System.EventArgs e) 138 { 139 lblPalindromeStatus.Text = ""; 140 141 string str = ""; 142 str = convertString(txtInputString.Text); 143 144 int left = 0; 145 int right = str.Length - 1; 146 147 while ((str[left] == str[right]) && (left < right)) 148 { 149 left++; 150 right--; 151 } 152 153 if (left < right) 154 { 155 lblPalindromeStatus.Text = "This phrase is NOT a Palindrome."; 156 } 157 else 158 { 159 lblPalindromeStatus.Text = "This phrase IS a Palindrome."; 160 } 161 } 162 163 // convert an input string to lower case and 164 // strip out embedded punctuation and spaces 165 private string convertString(string strInp) 166 { 167 string strTemp = ""; 168 string strOut = ""; 169 170 strTemp = strInp.Trim().ToLower(); 171 172 for (int i = 0; i= 'a') && (ch <= 'z')) 176 strOut += ch; 177 } 178 179 return strOut; 180 } 181 182 // when the user clicks on the "Exit" button - exit the application 183 private void btnExit_Click(object sender, System.EventArgs e) 184 { 185 this.Close(); 186 } 187 } 188 }