In: Computer Science
Can someone explain to me the program step by step next to each statement in the program by using comment \\ and do the program using the basic c++ cuz down program looked messy advance?
a. Request a five-letter string value from the console.
b. If the input string is not a five-letter word, print that the word is not a five-letter word.
c. If the input string is a five-letter word,determine if the word is or is not a Palindrome. Hint: String indexes can be used to compare letter values.
d. If the word is a Palindrome, print that it is a Palindrome.
Output Example (input is bold and italicized)
Enter a five-letter word: test
test is not a five-letter word.
Output Example (input is bold and italicized)
Enter a five-letter word: kayak
kayak is a palindrome.
#include<bits/stdc++.h>
using namespace std;
int main()
{
char string1[10000], string2[10000];
int i, j, length = 0, flag = 0;
cout << "Enter a Five-letter word: ";
cin>>string1;
length = strlen(string1)-1;
if((length+1) == 5)
{
for (i = length, j = 0; i >= 0 ; i--, j++)
string2[j] = string1[i];
if (strcmp(string1, string2))
flag = 1;
if (flag == 1)
cout << string1 << " is not a palindrome.";
else
cout << string1 << " is a palindrome.";
}
else
{
cout< }
return 0;
}
(Ans)
#include <iostream>
#include <cstring>//library for using strlen()
using namespace std;
int main(){
char word[30];
int i, length;
int flag = 0;
cout << "Enter a Five-letter word: ";
cin >> word;
length = strlen(word);//This gives us the length of the word given by the user
if(length==5)//If length of the given word is 5 then execute the steps in the if condition
{
for(i=0;i < length ;i++)
{
if(word[i] != word[length-i-1])//This step compares the word characters from start and end i.e if word is "Hello" then it compares first letter 'H' with last letter 'o'
//If they are same then it moves to the next set of words i.e 'e' and compares it with 'l' and so on. If any word mismatches then it breaks the loop and sets flag=1
{
flag = 1;
break;
}
}
if (flag)// If flag==1 that means that the characters of the input word from front and back doesn't match. So the given word is not a palindrome.
{
cout << word << " is not a palindrome" << endl;
}
else //If the characters match then the word is a palindrome.
{
cout << word << " is a palindrome" << endl;
}
}
else//if the length of the word is not 5 then printing the given word is not a 5 letter word.
{
cout<< word <<" is not a five-letter word.";
}
return 0;
}