views
Setting Up
Open the text editor you will use to write the program. You may use a simple word processor such as Notepad to write this code, but you will not get the added benefits of error warnings or automatic formatting of the code for readability.
Type the preprocessor directives that add the necessary libraries to your program. These statements tell the computer that your program will use two pre-existing libraries that are already built-in to C++. The iostream library contains code for input and output to the console. The string library contains code for creating and manipulating text strings. Including these libraries makes your programming life easier because you are taking advantage of the resources already available to you.
#include
Type the “using” statement for the namespace you will use (standard namespace). The text you type should appear on a new line. This text will inform the computer that you are using some abbreviated conventions for certain text that will appear later. For example, later on in this process, instead of typing “std::cout”, you will only have to type “cout”. Do not type the comments (statements that follow two forward slashes) as you proceed with this process.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 2 Version 2.jpg|center]]
#include
Building the Program
Type the main function. This program will only have one function, the main function, which is a part of every C++ program. The right curly brace will automatically appear on most text editors after you type the left one. The same is true of all symbols with an "opening" and "closing" case (such as parenthesis, "()", brackets, "[]", and curly braces, "{}"). All of the code you type within the main function is automatically indented to indicate its placement and improve readability. Make sure the rest of the code you type is within these two curly braces.
#include
Declare the necessary variables. Within the curly braces of the main function, type the new text shown below. This text establishes "str", "length", and "isPalindrome" as variables which store a text string, integer, and Boolean value respectively. The variable “str” will store the word that may or may not be a palindrome. The variable “length” will store the number of letters in the word. The variable “isPalindrome” will store whether or not the word is a palindrome. For the purpose of this program, we first assume the word is a palindrome, and then examine it to see if it is not a palindrome. If it is not a palindrome, we will change the value of “isPalindrome” to false.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 6 Version 2.jpg|center]]
#include
Type the prompt to the user asking for input. This text will inform the user to enter a word.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 8 Version 2.jpg|center]]
#include
Type the code to get input from the user. This text will take input from the user and put it in the variable “str” you created earlier.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 10 Version 2.jpg|center]]
#include
Type text to store the length of the word entered by the user in the variable “length”. The length of the word is needed so the computer knows when to stop looking through the letters in the word.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 12 Version 2.jpg|center]]
#include
Create a loop to examine the word letter by letter by typing the new text shown below. Put as simply as possible, this text creates a loop that will examine each letter with its corresponding mirror letter to see if they match. Since the number of examinations is half the size of the word, we divide the length by 2 in the code. When you type the left curly brace, the right one should automatically appear again. The next line of code should be typed within these new curly braces.
[[Image:Write a C++ Program That Determines if a Word Is a Palindrome or Not Step 14 Version 2.jpg|center]]
#include
Type the comparison statement within the curly braces you just typed. This statement carries out comparisons. A given letter, denoted “i", is compared with the letter in its mirrored position in the word. For example, in the word “madam”, the two m's will be compared, then the two a's, and so on.
#include
Type the statement to test the value of “isPalindrome”. If the word in question is a palindrome, the variable “isPalindrome” will still be true. Otherwise, it will be false. This “cout” statement displays the "true" instance to the user.
#include
Type the code to account for when the word is not a palindrome. If the word in question is not a palindrome, the variable “isPalindrome” will have a new value of “false” and the “else” statement will execute, displaying this fact to the user.
#include
Finishing Up
Type the return statement. This statement tells the computer that the program executed correctly. Make sure the final curly brace from the main function appears after this statement. If you are using a standard text editor, the indentation and spacing will happen automatically within the curly braces and this will be less likely to be a potential problem.
#include
Verify your code. You may run your code on your software to see that it works. How this is carried out will vary depending on your software.
Comments
0 comment