Question

C Programming Question: - Create a header file named lab2functions.h. In this file, place the following...

C Programming Question:

- Create a header file named lab2functions.h. In this file, place the following lines:
     #ifndef LAB2FUNCTIONS
     #define LAB2FUNCTIONS
                       // here is where your 4 function prototypes will go
     #endif

Note: Program the functions simple.

• Print a character array. Write a function called printCharArray that will print the elements of a
char array, 10 per line. The characters should be separated by one space. The parameters to the
function are:
1) an array of characters
2) the size of the array (of type size_t )

• Count the number of letters in a character array. Write a function named countLetters that will
count and return the number of letters (either uppercase or lowercase) that appear in the array. The
parameters to the function are:
1) an array of characters
2) the size of the array (of type size_t )

• Determine the number of elements that are the same in two arrays. Write a function named
numMatches that compares the elements in two arrays and counts how many elements are the
same. The function only compares array elements with the same index, array1[0] with
array2[0], array1[1] with array2[1], etc.
The arrays are assumed to have the same number of elements.
The parameters to the function are:
1) the first array of characters
2) the second array of characters
3) the size of the arrays (of type size_t )

• Replace all of the lowercase letters in the array with the corresponding capital letters. Write a
function named capitalize that looks for lowercase letters in the array. When a lowercase letter is
found, the array element should be changed to the corresponding capital letter. The parameters to
the function are:
1) an array of characters
2) the size of the array (of type size_t )

- Create a source file named lab2functions.c. In this file, place the implementations (the full header and body) of the four functions above. lab2functions.c should NOT have a main function. Start the file with comments and an include:

// header comments

#include "lab2functions.h"

// implementation of printCharArray

// implementation of countLetters

// implementation of numMatches

// implementation of capitalize

- Create a test program named lab2.c. In this file, place the main function as follows:

// header comments

#include "lab2functions.h"

. h not .c

#include <stdio.h>

#include <ctype.h>

int main(void) {

// include statements to completely and thoroughly

// test the functions

// use print statements to clearly state which function

// is being tested, print the contents of array(s) before

// and (if needed) after function calls so one can see whether

// functions worked correctly or not

} // end function main

You may initialize arrays for testing in your code or you may prompt the user to enter array characters if you like.

In total there should be three different files. lab2functions.h | lab2functions.c | lab2.c

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

output:

code:

File Name: lab2functions.h

//lab2functions.h

//header file and declarations as mentioned

#ifndef LAB2FUNCTIONS

#define LAB2FUNCTIONS

#include <stdio.h>

void printCharArray(char arr[], int size);

int countLetters(char arr[], int size);

int numMatch(char arr1[], char arr2[], int size);

void capitalise (char arr[], int size);

#endif

File Name: lab2functions.c

//lab2functions.c

//include the required header files

#include <stdio.h>

#include "lab2functions.h"

//define a method to print the character array

void printCharArray(char arr[], int size)

{

//initialise the variables

int i=0;

//for loop to loop until size

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

{

//print statement

printf("%c ", arr[i]);

}

printf(" ");

}

//method to check the characters

int isChar(char c)

{

return ( (c>='a' && c<='z') || (c>='A' && c<='Z'));

}

//T the number of letters in a character array.

int countLetters(char arr[], int size)

{

int i=0, T=0;

//for loop to loop until size

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

{

if(isChar(arr[i]))

{

T++;

}

}

return T;

}

//define a method to find the number of characters match

int numMatch(char arr1[], char arr2[], int size)

{

   //declare the required variables

int i=0, T=0;

//for loop to loop until size

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

{

//if loop to check the array

if(arr1[i] == arr2[i])

{

//increment the count variable

T++;

}

}

return T;

}

//define a method to convert the array to uppercase

void capitalise(char arr[], int size)

{

//for loop to loop until size

for(int i=0;i<=size;i++)

{

    //if loop for conversion

if(arr[i]>=97 && arr[i]<=122)

      {

        //convert to uppercase

    arr[i]=arr[i]-32;

      }

}

printf("%s",arr);

}

File Name: lab2.c

//lab2.c

//include the required header files

#include <stdio.h>

#include "lab2functions.h"

#include <string.h>

//Define main() function

int main()

{

//input 1

char inp1[7] = "labfun";

//input 2

char inp2[8] = "throug";

//to print the character array

printf(" The Character array of %s is: ", inp1);

printCharArray(inp1, strlen(inp1));

printf(" ");

//to count letters in character array

printf("The number of characters in %s is : ", inp1);

printf("%d ", countLetters(inp1, strlen(inp1)));

//to find the matching characters

printf("matching characters between %s and %s is : ", inp1, inp2);

printf("%d ", numMatch(inp1, inp2, strlen(inp1)));

printf(" ");

//conversion to upper case

printf("conversion to upper case of %s is : ", inp1);

capitalise(inp1, strlen(inp1));

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C Programming Question: - Create a header file named lab2functions.h. In this file, place the following...
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
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