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

Write the implementation for the three functions described below (in c source co

ID: 3723879 • Letter: W

Question

Write the implementation for the three functions described below (in c source code). The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently.
1) Write a print_string function that prints the characters in a string to screen on- by-one.
2) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if they are identical.
3) Write a is_palindrome function. The function is required to be case insensitive and should only consider the letters and the numbers in the input string, i.e., remove all fillers. Using this definition, the string “Madam, I’m Adam!” is considered a palindrome. The function is required to return 1 if the string is a palindrome and otherwise 0.


void print_string(char str[]);
int is_identical(char str1[],char str2[]);
int is_palindrome(char str[]);
void main(){
char str1[] = "Hello World";
char str2[] = "Madam, I'm Adam!";
char str3[] = "hello world";
print_string(str1);
printf(" ");
print_string(str2);
printf(" ");
(is_identical(str1,str2)) ? printf("Identical! ") : printf("No identical! ");
(is_identical(str1,str3)) ? printf("Identical! ") : printf("No identical! ");
(is_palindrome(str1)) ? printf("Palindrome! ") : printf("No palindrome! ");


(is_palindrome(str2)) ? printf("Palindrome! ") : printf("No palindrome! ");
}

Explanation / Answer

main.c:

#include <stdio.h>
#include <conio.h>
#include <string.h>

void print_string(char str[]);
int is_identical(char str1[],char str2[]);
int is_palindrome(char str[]);

void main()
{
char str1[] = "Hello World";
char str2[] = "Madam, I'm Adam!";
char str3[] = "hello world";
print_string(str1);
printf(" ");
print_string(str2);
printf(" ");
(is_identical(str1,str2)) ? printf("Identical! ") : printf("No identical! ");
(is_identical(str1,str3)) ? printf("Identical! ") : printf("No identical! ");
(is_palindrome(str1)) ? printf("Palindrome! ") : printf("No palindrome! ");
(is_palindrome(str2)) ? printf("Palindrome! ") : printf("No palindrome! ");
}

// functionfor displaying character by character and space between each character
void print_string(char str[]) {
  
for(int i=0; i < strlen(str); i++) {
printf("%c ", str[i]);
}
}

// function to check wether the strings are identical or not (Strings are Case sensitive)
int is_identical(char str1[],char str2[]) {
  
int res = 0;
if (strlen(str1) == strlen(str2)) { // if the lentgh of both strings is same then continue
for( int i=0; i < strlen(str1); i++) {
if(str1[i] != str2[i]){ // checking each character
res = 0;
break; // if any non matching character found stop checking and return 0
}else {
res = 1; // ALL characters matched return 1
}
}
}
return res;
}

int is_palindrome(char str[]) {
  
char reverseStr[strlen(str) -1]; // new char array to store the original string in reverse order
int flag = 1;
int index = 0;
  
// Removing the spaces and specail characters from the provided string
for(int i = 0,j; str[i] != ''; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') ||
str[i] == '') || (str[i] >= '0' && str[i] <= '9') )
{
for(int j = i; str[j] != ''; ++j)
{
str[j] = str[j+1];
}
}
}
  
// Converting the string to lower case as the string should not be case sensitive while checking palindrome
while (str[index] != '') {
if (str[index] >= 'A' && str[index] <= 'Z') {
str[index] = str[index] + 32;
}
index++;
}
  
// Copying the original string in reverse order
for (int i = strlen(str) - 1; i >= 0 ; i--)
{
reverseStr[strlen(str) - i - 1] = str[i];
}

// Check if the string is a Palindrome, if palindrome return 1 else 0
for (int i = 0; i < strlen(str) ; i++)
{
if (reverseStr[i] != str[i]) {
flag = 0;
}
}
  
return flag;
}

Out put:

H e l l o   W o r l d                                                                                                            

M a d a m ,   I ' m   A d a m !                                                                                                  

No identical!                                                                                                                    

No identical!                                                                                                                    

No palindrome!                                                                                                                   

Palindrome!