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

C programming array question 1. Log in to Linux. Make a new directory for this l

ID: 3874627 • Letter: C

Question

C programming array question
1. Log in to Linux. Make a new directory for this lab called Lab2. Place all of the files you create for this lab assignment in that directory 2. Create a header file named lab2functions h. In this file, place the following lines: #ifndef 1AB2FUNCTIONS #de fine L B2FUNCTIONS here is where your function prototypes go fendif 3. The following functions deal with arrays of characters I chose characters because I know you've had a lot of experience with integer arrays and it's time for a change For each of the functions below, place a one-line prototype in the header file Print a character array. Write a function called printCharArray that will print the elements of a char array, 10 per line. The values should be separated by one space. 1) an array of characters 2) the size of the array (an integer) Move letters to the front of a char array. Write a function named moveLetters that moves the letters toward the front of the array and moves everything else toward the back The respective order of the letters is not changed (See illustration below) Note: this is not "sorting the array The parameters to the function are 1) an array of characters 2) the size of the array (an integer) DO NOT make additional arrays. If you do, you'l receive a grade of zero for this function.

Explanation / Answer

lab2functions.h:

#ifndef LAB2FUNCTIONS
#define LAB2FUNCTIONS

#include <stdio.h>

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

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

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

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

#endif

lab2functions.c:


#include <stdio.h>
#include "lab2functions.h"


void printCharArray(char arr[], int size) {
int i=0;
for(i=0; i<size; i++) {
printf("%c ", arr[i]);
}
printf(" ");
}

int isChar(char c) {
return ( (c>='a' && c<='z') || (c>='A' && c<='Z'));
}

void moveLetters(char arr[], int size) {
int i, j;
for(i=0; i<size-1; i++) {
for(j=i; j<size; j++) {
if(!isChar(arr[j])) {
if(isChar(arr[j+1])) {
char c = arr[j+1];
arr[j+1] = arr[j];
arr[j] = c;
}
}
}
}
}

int countLetters(char arr[], int size) {
int i=0, count=0;
for(i=0; i<size; i++) {
if(isChar(arr[i])) {
count++;
}
}
return count;
}

int numMatch(char arr1[], char arr2[], int size) {
int i=0, count=0;
for(i=0; i<size; i++) {
if(arr1[i] == arr2[i]) {
count++;
}
}
return count;
}

  


main.c:

#include <stdio.h>
#include "lab2functions.h"
#include <string.h>

int main()
{
char s1[8] = "College";
char s2[10] = "Cou3r+se";
char s3[8] = "Abcdefg";
  
printf("Char array of %s: ", s1);
printCharArray(s1, strlen(s1));
printf(" ");
  
printf("countLetters on %s: ", s2);
printf("%d ", countLetters(s2, strlen(s2)));
  
printf("moveLetters on %s: ", s2);
moveLetters(s2, strlen(s2));
printf("%s ", s2);
  
  
printf("matching characters between %s and %s: ", s1, s3);
printf("%d ", numMatch(s1, s3, 7));
  
return 0;
}