Write a program compare.c that reads two strings from command line and outputs w
ID: 3816705 • Letter: W
Question
Write a program compare.c that reads two strings from command line and outputs whether one is the reverse of the other while ignoring the case. If the user does not run the program with the correct number of parameters, the program should output an error. Sample runs of the program are as follows: > compare Olla allo The strings are the reverse of each other > compare CAT tac The strings are the reverse of each other > compare Dog dragon The strings are not the reverse of each other > compare fog You must enter two words for comparison. PleaseExplanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int checkPalindrome(char *a, char *b)
{
int lengtha = strlen(a);
int lengthb = strlen(b);
if (lengtha != lengthb)
{
return 0;
}
int i;
for(i = 0; i < lengtha; i++)
{
if (tolower(a[i]) != tolower(b[lengthb -1 -i]))
{
return 0;
}
}
return 1;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("You must enter two words for comarison. Please try again. ");
exit(1);
}
if (checkPalindrome(argv[1], argv[2]))
{
printf("The strings are the reverse of each other ");
}
else
{
printf("The strings are not the reverse of each other ");
}
return 0;
}
Sample run
compare
You must enter two words for comarison. Please try again.
compare Olla allo
The strings are the reverse of each other
Please rate positively if this answered your query.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.