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
David has created a lot of styles and now his Quick Style Gallery contains styles he no longer uses.
shusha [124]

Answer:

Right-click the style in the Quick Styles Gallery, and select the Remove from Quick Style Gallery option.

Explanation:

3 0
4 years ago
Which fingers do you use to type the following letters rtgvf
Allushta [10]
Middle,index,index,thumb,middle
3 0
3 years ago
Write an if-else statement with multiple branches. If givenYear is 2100 or greater, print "Distant future" (without quotes). Els
Elanso [62]

Answer:

The program to this question in java can be given as

import java.util.Scanner;

//import package for take input from user.

public class Main //define class

{

public static void main(String[] args) // main function

{

Scanner input = new Scanner(System.in);

System.out.println("Enter a number:");

int givenYear=input.nextInt();

//if condition true output is "Distant future" (without quotes).

if (givenYear >= 2100)

{

System.out.print("Distant future");

}

// else if condition(2000-2099) true output is "21st century".

else if((givenYear >= 2000) && (givenYear <= 2099))

{

System.out.print( "21st century");

}

//else if condition (1900-1999) true output is "20th century".

else if((givenYear >= 1900) && (givenYear <= 1999)){

System.out.print( "20st century");

}

//else condition false output is "Long ago".

else{

System.out.print( "Long ago");

}

}

}

Explanation:

In this program we use the scanner class. Scanner class is used to take input from the user.When the user input the input the year. It hold on a variable(givenYear) that is integer type.Then we check the value between the ranges that is given in question.To check all the condition we use the if-elseif-else statement, and AND(&&) operator.AND operator is a logical operator that is used for compare two values together. The output of the program can be given as:

Output:

Enter a number: 2016

21st century

5 0
3 years ago
Technological improvements have allowed people to inhabit a wider variety of places more easily. Please select the best answer f
muminat

The statement ‘Technological improvements have allowed people to inhabit a wider variety of places more easily’ is true. Technology has made our lives easier and faster. You can just buy a land or house from the developer to obtain a place you want.

9 0
3 years ago
Read 2 more answers
Roy is preparing for his test in English. He creates lots of memory tools and studies using a circuit-learning schedule that he
kozerog [31]
He should get an good night sleep the day before the test, eat a filling breakfast, get to school on time, sit in his and relax. to take a test as much as you prepare for it you mustalso be calm; being frazzled will make it hard to concentrate. some streaches will release any stress and boost confidence. also letting some blood get to your brain will help you think better. to pass a test you must study, stable, relaxed, and alert.
8 0
3 years ago
Other questions:
  • The ________ model allows the owner of a resource to manage who can or cannot access the item. Owners maintain this access throu
    8·1 answer
  • Blood alcohol level is the ratio between the alcohol consumed and the blood in the body
    10·1 answer
  • You must establish credit in order to buy a house true or false
    5·2 answers
  • Climate is considered a(n) _______ limiting factor. (2 points)
    9·1 answer
  • Bulldog Holdings is a U.S.-based consumer electronics company. It owns smaller firms in Japan and Taiwan where most of its cell
    5·1 answer
  • What type of IPv6 address should you use when you have multiple routers on a subnet and want hosts to use the nearest router for
    10·1 answer
  • The methodology used by web project teams is usually:
    9·1 answer
  • Value of 3.0+4.0×6.0​
    14·1 answer
  • How do i solve this?
    11·1 answer
  • What are all the Answer Streaks (Fun Facts) for brainly?
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!