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

1. String Utilities In this exercise you will implement several utility function

ID: 3853776 • Letter: 1

Question

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 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 comparison function that compares two strings for equality. You cannot use the < string.h > 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.

e. char* strCat(char *str, char *str2)

- Concatenate the two strings “str” and “str2” and store that in a newly created dynamic string. The function should return this dynamic string. You cannot use the < string.h > concatenation functions.

For example, a call to this function on the string “Hello”, and “ World” will return a dynamically created string that contains “Hello World”.

f. void consonantVowel(char *str)
- This function will print out the number of consonants and vowels in the string. Note:

printing should be done in the function (notice the return type is void).

For example, a call to this function on the string “Hello”, will print Consonants: 3, Vowels 2.

Explanation / Answer

b.)

int numChar(const char *src, char c)
{int i,count=0;
for(i=0;i<strlen(src);i++)
{
if(src[i]==c)
count++;
}
return count;

}

c)


int isPalindrome(const char *src)
{
  
int l = 0;
int h = strlen(src) - 1;

while (h > l)
{
if (src[l++] != src[h--])
{
  
return 0;
}
}
return 1;
}

d.
int strCompare(const char *str, const char *str2)
{
  
int c = 0;

while (str[c] == str2[c]) {
if (str[c] == '' || str2[c] == '')
break;
c++;
}

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

e.

char* strCat(char *str, char *str2)
{
const size_t len1 = strlen(str);
const size_t len2 = strlen(str2);
char *result = malloc(len1+len2+1);
memcpy(result, str, len1);
memcpy(result+len1, str2, len2+1);
return result;
}

f.

void consonantVowel(char *str)
{
int consonant=0,vowel=0,i;
for(i=0; i<strlen(str); i++)
{
switch(str[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowel++;
break;
default:
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
consonant++;
}
}
}

  
printf("consonant: %d ", consonant);
printf(" vowel:%d ", vowel);
}