Write the code using c programming please In this exercise y

Write the code using c programming please.

In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file, string utils.c. As before, you should implement your own main test driver program to test your functions, but you need not hand it in. (a) void addChar(char *str, char c, int n) this function should add a char c at index n in string str. The following characters should be shifted up to make room for the inserted character. For example, a call to this function on the string \"Hello World\", \'p\', \'4\', would result in the string \"Hellpo World\" (b) int numChar(const char *src, char c) this example determines the number of character c appears in the string. It does not matter if the letter in the string is capitalized or not. for example, a call to this function on the string \"HellO World\", \'o\', would return 2 (c) int isPalindrome(const char *src) this example determines if the string src is a palindrome or not. Return 1 if the string is a palindrome and 0 if not. For example, a call to this function on the string \"testset\", will return 1 (d) int strCompare(const char *str, const char *str2) Write your own string compare function that compares two strings for equality. Make sure the compare function is case insensitive. Return 0 if the two strings are equal and 1 if they are not. For example, a call to this function on the string \"Hello world\", and \"Hello World\" will return 0 (e) char* strCat(char *str, char *str2) Write your own string without using the strcat() function that concatenates two strings. For example, a call to this function on the string \"Hello\", and \"World\" will return \"Hello World\" (f) void consonant Vowel (char *str) This function will print out the number of consonates and vowels in the string.

Solution

string_util.h

#include<stdio.h>

void addChar(char* str,char c,int pos)
{
   char *p;
int i;
int len=strlen(str)-1;
p = malloc(len + 2);
for (i = 0 ; i < pos ; ++i)
p[i] = str[i];
p[i++] = c;
for ( ; i < len + 2 ; ++i)
p[i] = str[i - 1];

strcpy(str,p);

   printf(\"%s\ \",str );

}

int numChar(const char* src,char c)
{
int count=0;

   while(*src!=\'\\0\')
   {
       char ch=tolower(*src);

      
       if(ch==c)
           count++;
       src++;
   }
   return count;
}

int isPalindrome(const char* src)
{
int j=strlen(src)-1;
printf(\"%d\ \",j );
int i=0;

   while(i<j)
   {
       char ch1=tolower(src[i]);
       char ch2=tolower(src[j]);

       if(ch1!=ch2)
           return 1;
       i++;
       j--;
   }
   return 0;


  
}

int strComapre(const char* str1,const char* str2)
{
   int c = 0;

while (tolower(str1[c]) == tolower(str2[c])) {
if (str1[c] == \'\\0\' || str2[c] == \'\\0\')
break;
c++;
}

if (tolower(str1[c]) == \'\\0\' && tolower(str2[c]) == \'\\0\')
return 0;
else
return -1;
}

char* strCat(char* str1,char* str2)
{
   int i = 0,len = 0;
while(*(str1+len)!=\'\\0\')
len++;
while(*(str2+i)!=\'\\0\')
{
*(str1+len) = *(str2+i);
i++;
len++;
}
*(str1+len) = \'\\0\';
return str1;
}

=======================================================

string_util.c

#include<stdio.h>
#include \"string_util.h\"


int main(int argc, char const *argv[])
{
   char str[]=\"Mama\";
   addChar(str,\'p\',4);

   printf(\"%d\ \",str);
   int c=numChar(str,\'o\');
   printf(\"%d\",c);
   int p=isPalindrome(str);
   printf(\"palind %d\ \",p );

   printf(\"string compare %d\ \",strComapre(\"Ak\",\"ak\"));
   char s1[]=\"Aksahay\";
   char s2[]=\"abhanga\";
   char* res=strCat(s1,s2);
   printf(\"%s\ \",res);

   return 0;
}

=====================================

Comment about work

Write the code using c programming please. In this exercise you will implement several utility functions involving strings. You will place all of your function
Write the code using c programming please. In this exercise you will implement several utility functions involving strings. You will place all of your function
Write the code using c programming please. In this exercise you will implement several utility functions involving strings. You will place all of your function

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site