Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone answer these questions with respect to the code? #include <iostream>

ID: 3860289 • Letter: C

Question

Can someone answer these questions with respect to the code?

#include <iostream>
#include<string>
using namespace std;

//function to check the string is palindrom or not
bool isPalindrome(string str)
{
int length = str.length();

for (int i = 0; i < length / 2; i++)
if (str[i] != str[length - 1 - i])
return false;

return true;
}

int main()
{
//declaration
string str;

//prompt to enter the string
cout << "Enter your word: ";
cin >> str;

//check the string
if(!isPalindrome(str))
cout << " " << str << " is not a palindrome!" << endl;
else
cout << str << " is a palindrome!" << endl;
return 0;
}

Explanation / Answer

Ans

1. A string is considered as an array of characters. and we know that array index start with 0. and to denote the end of a string null character is used that is ('') . So to get a character in a string just call str[0] or str[4] to get first and 5 th character respectively. while str is an array of strings.

2. We can determine the length of a string in two ways one is more simpler that is used here . i mean use the standerd string function that is length() that will return the length of a string . here int length= str.length();

The other way is , we have already say that it use '' to show the end of the string , and we can take each charecter as str[i] , so write a for loop or while

int i=0;

length=0;

while(str[i] != '') // it will continue untill str[i] equals null character()

{

length++;

i++;

}

then we get the length of string in the variable length.

3. Its so simple to check the characters at the begginig and end of a word is same , that is we have seen that , we can get the length of a string using the function length().

here in this code

int length = str.length(); // the length of the string will get in the variable length

that is if the string is "hello"

then the length will be 5. so the variable length will have 5

and the array string will be like this

str[0] -> 'h'

str[1] -> 'e'

str[2] -> 'l'

str[3] -> 'l'

str[4] -> 'o'

str[5] -> ''

So the first character is 'h' and last character is 'o' that is str[0] and str[4] . Our length variable has a value 5

that is 0-4;

so we can check for first and last characters are same or not like this

int length= str.length();

if(str[0] == str[length-1])

{

same

}

else // that is str[0] != str[length-1]

{

not same

}

4. For odd length words there will be no change in the case of first and last characters

because we already say it with a odd length word . if the length was 4 then check str[0] and str[3]

5. We need to check 5 charcters for word length is 10 that is   

word length is 10 that is 0,1 2,3,4,5,6,7,8,9

Then check

1. str[0] -> str[9]

2. str[1] -> str[8]

3. str[2] -> str[7]

4. str[3] -> str[6]

5. str[4] -> str[5]

We need to check 5 charcters for word length is 11 also   

word length is 11 that is 0,1 2,3,4,5,6,7,8,9,10

Then check

1. str[0] -> str[10]

2. str[1] -> str[9]

3. str[2] -> str[8]

4. str[3] -> str[7]

5. str[4] -> str[6]

then we don't wan't to check str[5] because that is in the middle so the no of checks is again 5

We can simply summarize it with ,that is if there are n numbers of characters in a word then we just want to check only n/2 characters.

6. Yes , because we check only n/2 characters for n number of charactes in a word. Since the length is integer

11/2 = 5

and 10/2 =5 also

so the middle character is not checked