1. String Utilities In this exercise you will implement several utility function
ID: 3853989 • 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 *sre, 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 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 0Explanation / Answer
Answer for the given Question:
Please see the below list of functions which are written as per given in question;
//addChar
void insert_char_malloc (char *str, char c, int pos)
{
char *p;
int i;
p = malloc(str + 2);
for (i = 0 ; i < pos ; ++i)
p[i] = str[i];
p[i++] = c;
for ( ; i < len + 2 ; ++i)
p[i] = str[i - 1];
free(str);
printf("Inserted string is %s ",p);
}
// strCat
char *strCat(char *str1,char *str2)
{
int i=0, j=0;
for(i = 0; str1[i] != ''; ++i);
for(j = 0; str2[j] != ''; ++j, ++i)
{
str1[i] = str2[j];
}
str1[i] = '';
return &str1;
}
// given string is palindromen or not
int isPalindrome(const char *str)
{
// Start from leftmost and rightmost corners of str
int l = 0;
int h = strlen(str) - 1;
// Keep comparing characters while they are same
while (h > l)
{
if (str[l++] != str[h--])
{
printf("%s is Not Palindromen", str);
return 1;
}
}
printf("%s is palindromen", str);
}
// Return the numChar times char is repeated
int numChar(const char *str,char c)
{
int i=0, count=0;
while(str[i]!='')
{
if(str[i] == c || str[i] == (c-32))
{
count++;
}
i++;
}
return count;
}
// strCompare will compare the two strings
int strCompare(const char* str1, const char* str2)
{
int i =0;
while (str1[i] == str2[i] && str1[i] != '')
i++;
if (str1[i] > str2[i])
return -1;
else if (str1[i] < str2[i])
return 0;
else
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.