void clear char s This function takes a character pointer a

void clear (char * s): This function takes a character pointer and frees the memory allocated to the pointer. char reverse (char s): This function takes a strings sfor a pointer to the input string) r a pointer to the input string). The variable s is a pointer to a character string (the one which the user enter in the main0). Similar to the copy function above, use malloc function to allocate memory for the reversed string and at the end return the pointer to the reversed string. int compare (char *a, char *b): This function takes two strings and compares two strings character by character from left to right. It returns the difference between the ascii value of first mismatched characters, otherwise zero if both strings are equal. A positive number if a is larger than b and a negative number if b is larger than a. Based on what is returned back, print an appropriate printf statement suggesting which string is bigger. int palindrome (char *a): This function takes in a string and checks if the string is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters that may be read the same way in either direction. E.g. civic, level, 1551 etc. these are all palindromes. This function returns 1 if the string is a palindrome 0 otherwise. PS: You can make this the smallest function in this lab. THINK!. THINK!!!. Read the document again for hint. and reverse with this string and print the results as shown in the sample output below. Then read in another string from the user for the compare function and call the compare function by main0: Similar to the prelab, read the first string from the user and call functions length, copy,

Solution

solution

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


char* reverse(char *k);
int compare(char *s,char *p);
int pallindrome(char *s);

void clear(char *s);
int main()
{
   int k;
   char *rever,*s1,*pallin;
   char *string1,*string2;
   printf(\"enter the two strings to compare and l string to compare:\");
     
   //to read strings
   string1 = malloc(256);
string2=malloc(256);
  
scanf(\"%255s\ \", string1); // Don\'t read more than 255 chars
scanf(\"%255s\ \", string2); // Don\'t read more than 255 chars
       int i=compare(string1,string2);
if(i==0)
{
    printf(\"two strings are equal %d\ \",i);
   
}else
printf(\"two string s are not equal %d\ \",i);

  
printf(\"enter the string to reverse\");
rever=malloc(256);
scanf(\"%s\",rever);
// Don\'t read more than 255 chars

   s1=reverse(rever);
     
   printf(\"the reversed string is %s\ \",s1);
     
  
   pallin=malloc(256);
scanf(\"%255s\ \", pallin); // Don\'t read more than 255 chars

     
k=pallindrome(pallin);
if(k==0)
   {
      printf(\"the given string is pallindrome\ \");
     
   }
else
{
    printf(\"the given string is not a pallindrome\ \");
   }
   clear(pallin);
     
return 0;

}

int pallindrome(char *r)
{
   int flag=0;
   int i;
   int lengt=strlen(r);
   for ( i=0;i<lengt;i++) {
       if(r[i]==r[lengt-i-1])
       flag=flag+1;
   }
   if(flag==lengt)
   return 0;
               else
   return 1;
  
      
}

void clear(char *s)
{
   printf(\" the memory is free of string : %s \ \",s);
   free(s);
}

char* reverse(char *s)
{
char *p;

p = malloc(256);
   
p=strrev(s);
  
return p;  
}

int compare(char *s,char *s1)
{
   int k=strcmp(s,s1);
  
   return k;
  
}

output

enter the two strings to compare and l string to compare:
john
lisa
level
two string s are not equal -1
the reversed string is level
enter the string to test pallindrome
madam
the given string is pallindrome
the memory is free of string : madam

 void clear (char * s): This function takes a character pointer and frees the memory allocated to the pointer. char reverse (char s): This function takes a stri
 void clear (char * s): This function takes a character pointer and frees the memory allocated to the pointer. char reverse (char s): This function takes a stri
 void clear (char * s): This function takes a character pointer and frees the memory allocated to the pointer. char reverse (char s): This function takes a stri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site