Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A palindrome is a phrase (i.e., a string of characters) that reads the same forw

ID: 3599134 • Letter: A

Question

A palindrome is a phrase (i.e., a string of characters) that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example: A man, a plan, a canal: Panama! is a palindrome. This lab consists of two programs that determine if a string represents a palindrome. The programs use two different string representations or types and different input schemes. You may use any code that you wish from the example programs as a part of either of your programs Do not include code from the examples that does not pertain to these labs Test Cases Valid Palindrome a man a plan a canal panama Invalid Palindrome a man a plan a canax panama Valid Palindrome never odd or evern nvalid Palindrome never odd or ven Program 1: Palindromes With The string Class Program 1 Requirements 1. Name your program (pal indrome.cpp 2. All strings in the program are implemented as instances of the C++ string class 3. Prompt for the user to enter a potential palindrome 4. Read the user input in to an instance of the string class (see string /Qe for an example) a. The test input will NOT contain punctuation characters (periods, commas, exclamation or question marks), etc.

Explanation / Answer

PROGRAM 1

CODE :

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

//function to check whether string is palindrome or not
bool checkPalindrome(string s){
  
int normalStr = 0;
int reverseStr = s.length()-1;
  
//check characters from starting of string and end of string
while(normalStr<s.length() && reverseStr>=0){
  
//if characters doesn't match return false as it is not palindrome
if(s[normalStr]!=s[reverseStr])
return false;
  
//increment star index and decrement end index
normalStr++;
reverseStr--;
}
  
//return true if reached her i.e all the characters matches
return true;
}

//test palindrome
int main() {
  
//take input from the user
string s;
cout<<"Enter a potential palindrome "<<endl;
getline (cin, s);
  
//remove whitespaces from the input string
for(int i = 0;i < s.length();i++){
while(s[i]==' ')
s.erase(i,1);
}
  
  
//check and print whether palindrome or not
if(checkPalindrome(s))
cout<<"String is palindrome";
else
cout<<"String is not palindrome";

  
return 0;
}

OUTPUT :

1 :
Enter a potential palindrome
a man a plan a canal panama
String is palindrome

2:
Enter a potential palindrome
never odd or ven
String is not palindrome

**If you have any query , please feel free to comment with details.
**Happy leaning :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote