String Utilities In this exercise you will implement several utility functions i
ID: 3684007 • 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, 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. void removeBlanks(char *src, char *dest) -This function removes any blanks from a given string. In particular, write the function which copies.src into dest but with all spaces removed. Note that, similar to the standard string functions, this function assumes that dest is large enough to hold the copied string. void replaceChar(char *src, char oldChar, char newChar) - this function replaces instances of a given character with a different character in a string. It will replace any instance of the character stored in oldChar with the character stored in newChar in the string src. int longestCommonPrefix(const char *a,const char *b) -write a function to compute the longest common prefix of two strings. For example, the longest common prefix of "global" and "glossary" is "glo". If two strings have no common prefix, then the longest common prefix is the empty string. The return value of this function should be the length of the longest common prefix (0 if none). char * flipCase(const char *src) - this function creates a copy of a given string and then flips the case of each character: upper case letters should become lower case and vice versa. For example, the string "GNU Image Processing Tool-Kit" should become "gnu iMAGE PROCESSING tOOL-klT."Explanation / Answer
/**** string_utils.h ****/
#ifndef STRINGUTILS_H
#define STRINGUTILS_H
void removeBlanks ( char*src, char *dest);
void replaceChar(char *src, char oldChar, char newChar);
char *flipCase(const char *src);
int longestCommonPrefix(const char *a, const char *b);
#endif
/*** string_utils.c ****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_utils.h"
void removeBlanks ( char*src, char *dest)
{
int length = strlen(src);
int i =0;
int j =0;
for (i = 0; i < length; ++i)
{
if(src[i] != ' ')
{
dest[j] = src[i];
j++;
}
}
dest[j] = '';
}
void replaceChar(char *src, char oldChar, char newChar){
//get length of source string.
int length = strlen(src);
//checks and manipulates the string.
int i;
char c;
for (i = 0; i < length; i++){
if (src[i] == oldChar){
src[i] = newChar;
}
}
}
char *flipCase(const char *src)
{
char *output;
output = malloc(strlen(src) + 1);
//Copy source to output
strcpy(output,src);
int i = 0;
//Check if the case is lower or upper
while(output[i] != '')
{
if(isalpha(src[i]))
{
if (isupper(src[i]))
{
//Convert to lower case and increment i
output[i]= tolower(src[i]);
i++;
}
//if it's lower case
else
{
if (islower(src[i]))
{
//Convert to upper and increment i
output[i]=toupper(src[i]);
i++;
}
}
}
else i++;
}
return output;
}
int longestCommonPrefix(const char *a, const char *b)
{
int size,sizea, sizeb;
int com=0;
int i=0;
sizea=strlen(a);
sizeb=strlen(b);
if(sizea==0||sizeb==0){
return com;}
if(sizea>=sizeb){
size=sizea;}
if(sizea<sizeb){
size=sizeb;}
for(i=0; i<size; i++){
if(a[i] != b[i]){
return com;
break;}
com++;}
return com;
}
/*** string_test.c ***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_utils.h"
int main()
{
char src[30] = "GNU Image Processing Tool-Kit";
char str[30];
char dest[30] = "GNU Image is good tool-kit";
printf("Remove Blanks in string GNU Image Processing Tool-Kit: ");
removeBlanks(src,str);
printf("%s ",str );
printf("Replace character e in string GNU Image Processing Tool-Kit by character p: ");
replaceChar(src,'e','p');
printf("%s ",src);
printf("Flip characters of string GNU Image Processing Tool-Kit: ");
printf("%s ",flipCase(src) );
printf("longestCommonPrefix is: ");
printf("%d ",longestCommonPrefix(src,dest));
return 0;
}