1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
olganol [36]
4 years ago
6

Write a C++ program that determines if a given string is a palindrome. A palindrome is a word or phrase that reads the same back

ward as forward. Blank, punctuation marks and capitalization do not count in determining palindromes. Here is some examples of palindromes:
Radar
Too hot to hoot
Madam!I'm adam
Computers and Technology
1 answer:
Ostrovityanka [42]4 years ago
6 0

The cpp program to determine if the given string is palindrome, is shown below.

The program has comments at places to indicate the logic.

#include <iostream>

#include <string.h>

using namespace std;

int main() {

// string is taken as an array of characters of size 50

char str1[50];

int i, len, flag;

cout<<"Enter any string to check for palindrome"<<endl;

cin>>str1;

// length of input string is stored in variable len to be used in the loop

len = strlen(str1)-1;

for(i=0; i<=len; i++)

{

// if string is palindrome, value of flag variable is initialized to 0 else 1

   if(str1[i]==str1[len-i])

        flag = 0;

           else

                flag = 1;

}

if (flag == 0)

       cout << str1 << " is a palindrome";

   else

       cout << str1 << " is not a palindrome";      

// returns integer value since main has return type of integer  

   return 0;

}

Explanation:

The header files for input and output and string are imported.

#include <iostream>

#include <string.h>

The string is taken as an array of type char having adequate size of 50.

char str1[50];

The length of the input string is calculated as

len = strlen(str1)-1;

Within a single for loop using a single variable and the length of the string, we check whether the string is palindrome or not.

      for(i=0; i<=len; i++)

{

   if(str1[i]==str1[len-i])

        flag = 0;

            else

                flag = 1;

}

The int flag is declared but not initialized.

If the string is palindrome, the value of flag variable becomes 0. Otherwise, the value of flag variable will be initialized to 1.

Depending on the value of flag variable, the message is displayed if the input string is palindrome or not.

The above program works for all strings irrespective of special characters which may be included in the string.

The above program also works for the examples of input string given in the question.

You might be interested in
Which methods will remove filters? Check all that apply.
dezoksy [38]

Answer:

The last three options are the correct ones

Explanation:

6 0
3 years ago
Read 2 more answers
Yooooooooooooooo
patriot [66]

Answer:

A-A Boolean search

Explanation:

8 0
3 years ago
Read 2 more answers
PLEASEEEE HELPP ILL MARK BRAINLIEST!! in a presentation outline, each slide must represent a main point in the outline. true or
patriot [66]

Answer: true

Explanation: hope this helps

6 0
3 years ago
A user can set the security and privacy settings on what is displayed in the message bar from the ________ within the options me
djverab [1.8K]
Depends on the Operating System
6 0
3 years ago
Where was the first type of computer found
Dennis_Churaev [7]
The ENIAC was invented by J. Presper Eckert and John Mauchly at the University of Pennsylvania and began construction in 1943 and was not completed untill 1946. It occupied about 1,800 square feet and used about 18,000 vacuum tubes, weighing almost 50 tons.
8 0
3 years ago
Read 2 more answers
Other questions:
  • Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour
    14·1 answer
  • Explain why computer users should use the cloud?
    14·1 answer
  • Victor has been murdered, and Art, Bob, and Carl are suspects. Art says he did not do it. He says that Bob was the victim's frie
    14·1 answer
  • I NEED HELP NOW PLEASE!!!!!!
    6·2 answers
  • Acomputer with a domain name is called a
    8·1 answer
  • Money from a Coverdell Education Savings Account can be used for:
    11·1 answer
  • In the United States, everyone is guaranteed work true or false
    13·1 answer
  • Where could an identity theft access your personal information?
    9·2 answers
  • Smartphones are the top target for _______ that locks a device and then requests payment for an unlocking code.
    5·1 answer
  • What are the 3 rules of music<br><br> ps: there is no music subject so i had to put something else
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!