Write the code using c programming please. In this exercise you will implement s
ID: 3931615 • Letter: W
Question
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.Explanation / Answer
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!='')
{
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] == '' || str2[c] == '')
break;
c++;
}
if (tolower(str1[c]) == '' && tolower(str2[c]) == '')
return 0;
else
return -1;
}
char* strCat(char* str1,char* str2)
{
int i = 0,len = 0;
while(*(str1+len)!='')
len++;
while(*(str2+i)!='')
{
*(str1+len) = *(str2+i);
i++;
len++;
}
*(str1+len) = '';
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.