String Utilities In this exercise you will implement several utility functions i
ID: 3853991 • Letter: S
Question
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 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
1.check palindrome or not:-
#include <stdio.h>
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
int main()
{
char string[100];
int result;
printf("Input a string ");
gets(string);
result = is_palindrome(string);
if ( result == 1 )
printf(""%s" is a palindrome string. ", string);
else
printf(""%s" is not a palindrome string. ", string);
return 0;
}
int is_palindrome(char *string)
{
int check, length;
char *reverse;
length = string_length(string);
reverse = (char*)malloc(length+1);
copy_string(reverse, string);
reverse_string(reverse);
check = compare_string(string, reverse);
free(reverse);
if ( check == 0 )
return 1;
else
return 0;
}
int string_length(char *string)
{
int length = 0;
while(*string)
{
length++;
string++;
}
return length;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '';
}
void reverse_string(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '' || *second == '' )
break;
first++;
second++;
}
if( *first == '' && *second == '' )
return 0;
else
return -1;
}
2.compare two strings :-
#include<stdio.h>
3.count number of characters in string:-