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

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

ID: 3854297 • Letter: P

Question

Programs

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

stringUtils.h:

#ifndef STRING_UTILS

#define STRING_UTILS //fucntion declaration for all function

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)

a. void addChar(char *str, char c, int n);

#include <stdio.h>

#include<string.h>

#define MAX_SIZE 100 //Maximum size of the string

/* Function declaration */

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

int main() {

char str[100];

strcpy(str,”This is string.h library function”);

puts(std);

memset(str,’$’,7);

puts(str);

return(0);

}

b.int numChar(const char *src, char c)

Code:

#include <stdio.h>

#include <ctype.h>

/* function declaration */

int numChar(const char *, char);

int main() { char chr, str[100];

/* reading the string */

printf(" Enter a string : ");

scanf("%[^ ]s",str);

/* reading a character from the string*/

}

c. int isPalindrome(const char *src)

This example is used to determines if the string src is a palindrome or not. So here is the code for that:

#include <stdio.h>

#include <string.h>

int main()

int isPalindrome(const char *src)

{

   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome ");

   gets(a);

   strcpy(b,a);

   strrev(b);

   if (strcmp(a,b) == 0)

      printf("Entered string is a palindrome. ");

   else

      printf("Entered string is not a palindrome. ");

   return 0;

}

Output :

Enter the string to check if it is a palindrome

testset

Entered string is a palindrome

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

code:

//stringcompare.c

#include<stdio.h>

//method for comparing string

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

{

int equal=0;

int index=0; //iterating through string while no function is case insensitive

char a[100], b[100];

   printf("Enter the first string ");

   gets(a);

   printf("Enter the second string ");

   gets(b);

   if (strcmp(a,b) == 0)

      printf("Entered strings are equal. ");

   else

      printf("Entered strings are not equal. ");

   return 0;

}

Output:

Enter the first string

number

Enter the second string

Number

Entered strings are not equal

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

code:

#include <stdio.h>

#include <stdlib.h>

int strLen(char* str)

{

int cnt = 0;

char str[10] = "Hello";

     char str2[10] = "World";

     strcat(str,str2);

     printf("Output string after concatenation: %s", str);

     return 0;

}

Output

Output string after concatenation: HelloWorld

f. void consonantVowel(char *str)

code:

#include<stdio.h>

void consonantVowel(char *str);

// function declaration

int main()

{

char str[100]; // variable declaration

int i, len, vowel, consonant;

  printf("Enter any string: ");

    gets(string);

    vowel = 0;

    consonant = 0;

    len = strlen(string);

    for(i=0; i<len; i++)

    {

        /*

         * If the current character(string[i]) is a vowel both upper and lowercase characters

         */

        if(string[i] =='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A' || string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U')

        {

            vowel++;

        }

        else if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))

        {

            consonant++;

        }

    }

    printf("Total number of vowel = %d ", vowel);

    printf("Total number of consonant = %d ", consonant);

    return 0;

}