Question

Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\nEnter a string : ");

scanf("%[^\n]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\n");

   gets(a);

   strcpy(b,a);

   strrev(b);

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

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

   else

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

   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\n");

   gets(a);

   printf("Enter the second string\n");

   gets(b);

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

      printf("Entered strings are equal.\n");

   else

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

   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\n", vowel);

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

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

    Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • You are to implement a MyString class which is our own limited implementation of the std::string...

    You are to implement a MyString class which is our own limited implementation of the std::string Header file and test (main) file are given in below, code for mystring.cpp. Here is header file mystring.h /* MyString class */ #ifndef MyString_H #define MyString_H #include <iostream> using namespace std; class MyString { private:    char* str;    int len; public:    MyString();    MyString(const char* s);    MyString(MyString& s);    ~MyString();    friend ostream& operator <<(ostream& os, MyString& s); // Prints string    MyString& operator=(MyString& s); //Copy assignment    MyString& operator+(MyString&...

  • C Programming Language only please. Your help is greatly appreciated. Will give thumbs up for quality...

    C Programming Language only please. Your help is greatly appreciated. Will give thumbs up for quality work. Lab 8 (this is to be turned in) Palindrome is a string that is the same as backwards or forwards “otto” “ababa” “noon” “ababbbbaba” In this lab we will work with strings (so we will be using character arrays). 1. Write a program that reads two strings strl and str2 from the keyboard 2. Print both strings 3. Write a function computeLength and...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • In C++ Write a program that will read a string, call 2 functions to modify the...

    In C++ Write a program that will read a string, call 2 functions to modify the string, and then print the final result. The first function should take a string parameter and return the string without any vowels. The second function should take a string and double every letter (which should be all consonants at this point). Be sure to: You must use more than [ ] and at You must use erase, insert, replace, find, and/or substr to make...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT