COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
checkpoint6.7.cpp File Reference
#include <iostream>
#include <cctype>
#include <string>
Include dependency graph for checkpoint6.7.cpp:

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( void )

Definition at line 6 of file checkpoint6.7.cpp.

6 {
7 string input;
8 getline(cin, input);
9 for (unsigned int i = 0; i < input.length(); i++){
10 if (!isalpha(input[i])){
11 input.erase(i, 1);
12 i--; // Decrement i to account for the removed character
13 }
14 }
15 // palindrome check
16 bool isPalindrome = true; // Assume the string is a palindrome
17 // Compare the first half of the string to the second half
18 for (unsigned int i = 0; i < input.length() / 2; i++){
19 // If the characters are not the same, the string is not a palindrome
20 if (tolower(input[i]) != tolower(input[input.length() - 1 - i])){
21 isPalindrome = false;
22 // Break out of the loop early if the string is not a palindrome
23 break;
24 }
25 }
26 cout << "The input string is " << (isPalindrome ? "" : "not " ) << "a palindrome." << endl;
27 return 0;
28}