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]
2 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]2 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
Given num_rows and num_cols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print
soldi70 [24.7K]

Answer:

The program written in Python is as follows

Explanation:

4 0
3 years ago
Attribute variables have the same meaning as participants variables. is this true or false?​
zysi [14]

Answer:

true

Explanation:

because I said so dude

5 0
3 years ago
PLS HELP IM SO DESPERATE!! 40 points
Hunter-Best [27]
You would reboot the computer install a VPN and a virus detector
8 0
3 years ago
A user is experiencing slow performance with their computer. A technician suspects the computer has a virus and runs antivirus s
IceJOKER [234]

Answer:

The answer is "Option A"

Explanation:

Escalation is the process of manipulating a bug, design failure in software program to obtain elevated access to the resources, which are usually shielded from the user, and wrong choices can be described as follows:

  • In option B, It is wrong because It can't provide any type of problem-solving.
  • In option C, It is wrong because it is a searching module.
  • In option D, It is wrong because it is used to verify the system.
6 0
3 years ago
Dash transfers several bits of data together at one time<br>​
Andreas93 [3]

Answer:

Synchronous data transmission is a data transfer method in which a continuous stream of data signals is accompanied by timing signals (generated by an electronic clock) to ensure that the transmitter and the receiver are in step (synchronized) with one another. The data is sent in blocks (called frames or packets) spaced by fixed time intervals.

6 0
2 years ago
Other questions:
  • Sarah is creating and formatting a newsletter for her school. Which page layout rules should she consider when doing this?
    13·2 answers
  • When did the digital revolution begin
    10·1 answer
  • Ar count = 10;
    13·1 answer
  • Which of the following lines of code is syntactically correct?
    14·1 answer
  • I'm confused. Are , , and elements or nodes? What exactly defines nodes? What defines elements?
    13·1 answer
  • What did do you do if you made a mistake on a computer?
    14·2 answers
  • This isn't a question
    8·1 answer
  • The question of ________ arises when considering the way in which online marketers gather consumers’ information over the Intern
    6·1 answer
  • What is a Computer ?and it's demerits​
    13·2 answers
  • Can someone send me reference on communication, importance , communication cycle , pros and cos​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!