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
What ethical concerns might arise from applying new IT to law enforcement?
pentagon [3]

Answer:

One ethical concern that might occur is the rise of bias in the police department. It would be possible for a disgruntled or frustrated cop to lean into new IT and be able to take advantage of a past suspect or an "enemy" of theirs such as a neighbor or even a coworker.

6 0
3 years ago
False when you tap or click the ‘decrease font size’ button, excel assigns the next lowest font size in the font size gallery to
castortr0y [4]
That is actually true. In order to format the font, margins and alignment of a form you need to s<span>elect the control that you want to format, then right-click the selection, and then click Format Control. What you do next is that on the Font tab you select the font type, font style, font size, other formatting options for the selected text and in order to finish to press ok </span>
6 0
3 years ago
Why is statistics important?
kenny6666 [7]

The answer is A.؜؜؜؜؜؜؜؜؜؜؜؜؜؜؜؜؜؜

7 0
3 years ago
What do newly PivotTables look like?
adell [148]

Answer:

Answer is in attached image!

Explanation:

8 0
3 years ago
You want to add a new Product and Services entry to your client's QuickBooks Online company. The service is installation of a se
Kisachek [45]

Answer:  

The correct answer is Option 3: "Select Service"  

You have to select "Service" in the Second Step.  

Only after that, you'll be able to name the Service in the following step.  

Other options are invalid for a given question.

6 0
3 years ago
Other questions:
  • Numeric data is stored in ___________ for direct processing.
    10·2 answers
  • Can someone help me, please
    7·1 answer
  • A proprietary operating system designed for mobile devices, with associated libraries, user interface, frameworks and reference
    9·1 answer
  • Which of the following people was a member of FFA?
    7·1 answer
  • The decimal number 3 is ___ in binary the 2s column plus the 1s column.
    11·1 answer
  • Which network component is used to connect one network to another? Which component delivers the message to the destination compu
    5·1 answer
  • Combining two or more cells to make one is called​
    8·1 answer
  • You have a passage of text that needs to be typed out, but some of the letter keys on your keyboard are broken! You're given an
    14·1 answer
  • Heeeeeeeeeeeeeeeeeeeeeeeeelp pat.2
    12·1 answer
  • 100 POINTS!!!!!!!!
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!