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

Programs tsing C progem 1. String Utilities In this exercise you will implement

ID: 3853667 • Letter: P

Question

Programs tsing C progem 1. String Utilities 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 named string.utils.c. 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 e) - 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 funetion on the string "HellO World", 'o, would return 2. c. int isPalindrome(const char 'sre) - 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 comparison function that compares two strings for equality. You cannot use the string comparison functions. 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.

Explanation / Answer

Here is the code for string_utils.h:

#ifndef STRING_UTILS
#define STRING_UTILS
#include <stdio.h>

void addChar(char *str, char c, int n);
int numChar(const char *src, char c);
int isPalindrome(const char *src);
int strCompare(const char *str, const char *str2);
char* strCat(char* str, char* str2);
void consonantVowel(char* str);
#endif

And the code for string_utils.c is:

#include "string_utils.h"

void addChar(char *str, char c, int n)

{

char temp;

while(*(str+n) != '')

{

temp = *(str+n);

*(str+n) = c;

n++;

c = temp;

}

*(str+n) = c;

*(str+n+1) = '';

}

int numChar(const char *src, char c)

{

int count = 0;

int i = 0;

char temp;

if(c >= 65 && c <= 90)

c = c + 32;

while(*(src+i) != '')

{

temp = *(src + i);

if(temp >= 65 && temp <= 90)

temp = temp + 32;

if(temp == c)

count++;

i++;

}

return count;

}

int isPalindrome(const char *src)

{

int count = 0;

while(*(src+count) != '')

count++;

for(int i = 0; i < count/2; i++)

if(*(src+i) != *(src+count-i-1))

return 0;

return 1;

}

int strCompare(const char *str, const char *str2)

{

char temp1 = *str, temp2 = *str2;

int i = 0;

while(temp1 != '' && temp2 != '')

{

temp1 = *(str + i);

if(temp1 >= 65 && temp1 <= 90)

temp1 = temp1 + 32;

temp2 = *(str2 + i);

if(temp2 >= 65 && temp2 <= 90)

temp2 = temp2 + 32;

if(temp1 != temp2)

return 1;

i++;

}

if(temp1 == temp2)

return 0;

else

return 1;

}

Sorry. I could only implement the first 4 functions, due to lack of time.