How to Write a C++ Program That Determines if a Word Is a Palindrome or Not
Method 1 of 3:
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 #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.
#include #include //new text appears below this line using namespace std;
Method 2 of 3:
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 #include using namespace std; //new text begins here int main() { } //new text ends here
- 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.
#include #include using namespace std; int main() { //new text begins here string str; int length; bool isPalindrome = true; //new text ends here }
- Type the prompt to the user asking for input. This text will inform the user to enter a word.
#include #include using namespace std; int main() { string str; int length; bool isPalindrome = true; //new text begins here cout << "Enter a word: "; //new text ends here }
- 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.
#include #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; //new text begins here getline(cin, str); //new text ends here }
- 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.
#include #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); //new text begins here length = str.length(); //new text ends here }
- 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.
#include #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); length = str.length(); //new text begins here for (int i = 0; i < (length / 2); i++) { } //new text ends here }
- 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 #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); length = str.length(); for (int i = 0; i < (length / 2); i++) { //new text begins here if (str[i] != str[(length - 1) - i]) isPalindrome = false; //new text ends here } }
- 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 #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); length = str.length(); for (int i = 0; i < (length / 2); i++) { if (str[i] != str[(length - 1) - i]) isPalindrome = false; } //new text begins here if (isPalindrome == true) cout << str << " is a palindrome" << endl; //new text ends here }
- 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 #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); length = str.length(); for (int i = 0; i < (length / 2); i++) { if (str[i] != str[(length - 1) - i]) isPalindrome = false; } if (isPalindrome == true) cout << str << " is a palindrome" << endl; //new text begins here else cout << str << " is not a palindrome" << endl; //new text ends here }
Method 3 of 3:
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 #include using namespace std; int main() { string str; int length; bool isPalindrome = true; cout << "Enter a word: "; getline(cin, str); length = str.length(); for (int i = 0; i < (length / 2); i++) { if (str[i] != str[(length - 1) - i]) isPalindrome = false; } if (isPalindrome == true) cout << str << " is a palindrome" << endl; else cout << str << " is not a palindrome" << endl; //new text begins here return 0; //new text ends here }
- 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.
5 ★ | 1 Vote
You should read it
May be interested
- How to write fractions in Word is very simplehow to write fractions in word is extremely simple, during the editing process, sometimes we will have to enter mathematical formulas and include fractions.
- How to write vertically, rotate text vertically in Wordaccording to some requirements, you need to write vertical letters in word, but you do not know how to write vertical letters? to be able to write vertically in word, there are many ways, you can refer to the ways guided in this article.
- How to write mathematical formulas in Word 2010you often edit text in word 2010, but you do not know how to write mathematical formulas in word 2010 like? so invite you to refer to the following article to learn how to write mathematical formulas in word 2010.
- Instructions to put Tab in Wordin word, there are many tabs that can help you a lot in the process of drafting different types of documents, helping you to write text and write text at any position but still in line and still aligned.
- Write a program to check duplicate values in Pythonin this article, tipsmake.com will learn with you how to write a program to determine duplicate values in the python programming language.
- 6 easiest ways to write fractions in Word on versions 2016, 2010, 2013, 2007writing fractions in word has 2 different ways of expressing it: the math fraction type with a dash dividing the numerator and denominator, the second type is / like writing 1/2 in word.
- How to quickly write arrow signs in Wordhow to quickly write arrow signs in word. the arrow mark is one of the more common signs we use in word to introduce, guide, and guide various steps. the arrow in word is available in the symbol set, we have to start and install to be able to use it.
- How to write fractions in Wordthe easiest way for you to write fractions in word quickly and without using other add-ins, you do the following:
- How to write text on drawings in Word is very easyin word there are times when we have to insert drawings to represent diagrams and require content inside. if you do not know how to insert text into drawings in word, please refer to the following article.
- How to insert and write arrow symbols (↑ ↓ ← ↕↔ → ↘↗↙↖) in Wordarrow icons are often used in word. so how to insert arrow icons in word?