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
ikadub [295]
3 years ago
6

Write a bool function isPalindrome that takes one string parameter and returns true if that string is a palindrome and false oth

erwise. c++

Computers and Technology
1 answer:
kkurt [141]3 years ago
7 0

Answer:

Here is the bool function isPalindrome:

bool isPalindrome(string s)

{  bool palindrome;      

 int j;

 int len = s.length();

 for (int i =0, j= len-1; i < len/2; i++, j--)  

   if (s[i] == s[j])

     palindrome = true;  

   else

     palindrome = false;

 return palindrome; }

Explanation:

The bool function isPalindrome takes a string as parameter and returns true if the string is a palindrome or false if the string is not palindrome. The string variable s holds the string value. In function isPalindrome, there is a bool type variable palindrome. bool is a data type which is used to hold values true or false which is then stored in the palindrome variable.

Next it is being found that how many characters are there in the input string using length() function which returns the length of the string. The length of the string is stored in int type variable len.

Next the for loop starts which has two variables i and j where i starts at 0 position of the input string and j starts at the last position of the string. These two variables move or traverse along the string this way. i variable stops when half the length of the input string is reached. At the end of every iteration i moves forward by the increment of its value to 1 and j moves backwards by the decrement  of its value to 1.

Every the body of  the loop executes, there is an if condition in the body of the loop which checks if the string is a palindrome. This is checked by matching the character at position/index i with the character at position j of the string. This will continue character by character until the i reached half the length of the input string and if every character in i was matched to every character in j then palindrome = true; statement is executed which returns true as the string is a palindrome otherwise palindrome = false; is returned as the string is not a palindrome.

For taking input string from the user and to check if the function isPalindrome() works correctly a main() function is written which takes input string from the user and reads in variable str calls isPalindrome() function. If the string is palindrome then the cout statement in main() function prints the message: String is a palindrome and if the input string is not a palindrome the the following message is displayed in the output screen: String is not a palindrome

int main()

{

string str;

cout<<"Enter a string: ";

cin>>str;

if(isPalindrome(str))

cout<<"String is a palindrome";

else

cout<<"String is not a palindrome";

}

The screenshot of the program along with the output is attached.

You might be interested in
Which of the below statements describes the nature of HTML elements - check as many as apply
slava [35]

Answer:

The answer to this question is option 1,2 and 4.

Explanation:

Some html elements does not contain anything or having no content are called empty html elements.for ex:- <br>,<hr> etc.

HTML elements are of two types block and inline.block elements starts with new line for ex:-div,body etc.Inline elements does not starts with new line.for ex:-<style>,<meta>,<head>etc.

Html elements also contains attributes that modify the element for ex:-

<body style="background-color:blue;">

where style is an attribute.

5 0
3 years ago
Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Part I so that it throws an InvalidTes
Elan Coil [88]

Answer:

See Explaination

Explanation:

package testscores;

import java.util.ArrayList;

import java.util.List;

public class TestScores {

public List<Integer> scorearray=new ArrayList<>();

public TestScores(List<Integer> scores) throws InvalidTestScore{

this.scorearray=scores;

for(int i=0;i<scorearray.size();i++){

if(scorearray.get(i)>100 || scorearray.get(i)<0){

throw new InvalidTestScore(this.scorearray.get(i));

}

}

}

public double average(){

int tot=0;

for(int i=0;i<this.scorearray.size();i++){

tot=tot+this.scorearray.get(i);

}

return tot*(1.0)/(this.scorearray.size());

}

class InvalidTestScore extends Exception

{

private double amount;

public InvalidTestScore(int Score)

{

System.out.println("Invalid Score "+Score);

}

}

}

8 0
3 years ago
On the MarketingConsultants worksheet, in cells D10:H13, use a PMT function to calculate the end of the month payment amount. En
vitfil [10]

Answer:

sdddddddde22

Explanation:

5 0
2 years ago
Harmful programs used to disrupt computer operation, gather sensitive information, or gain unauthorized access to computer syste
Wewaii [24]

Answer:

B. Malware

Explanation:

Malware is general to most harmful programs on a computer, as Adware is a potentially unwanted program that is designed to advertise other programs or services. And ransomware is a program that will encrypt important user files such as; documents, pictures, and videos. Spyware is a program that is designed to spy on users, examples are; using your camera without your consent, key-loggers, and taking screenshots of our desktop. Thus i believe it is malware because the question covers multiple categories.

6 0
3 years ago
Please explain why an operating system makes a great attack target.​
BartSMP [9]
The operating system is the fundamental controller of all system resources
Operating system popularity is one major reason why hackers attack
Window is a big target because it powers the vast majority of the world’s desktop computers and laptops.
To steal personal and protected health information
To steal user credit card numbers and other financial data
To harm the system or disrupt business operations of any organization
To gain competitive edge over opponents
To spoil the reputation of an organization

If you want answer to question 19, put a Thanks and comment that you want answer to question 19
8 0
3 years ago
Other questions:
  • In 125 words describe the steps to active listening.
    6·1 answer
  • Rapid development programming languages eliminate the possibility of having bugs in code. True or False
    8·1 answer
  • What is transaction model?
    9·2 answers
  • Why do some people think the global<br> economy is good for the United States?
    10·1 answer
  • Fill in the blank: _________ is Google’s machine-learning artificial intelligence system that interprets people’s searches to fi
    9·1 answer
  • Which code must be included to use Turtle Graphics?
    14·2 answers
  • The average price of milk increased from $3.00 last year to $3.50 this year. This is most likely due to:
    14·1 answer
  • What is the principal goal of data science?<br>​
    5·1 answer
  • Tier 1 networks form the internet ____.​
    15·1 answer
  • A technology _________ occurs when the ability of a company to operate is impaired because of a hardware, software, or data outa
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!