~hamblingreen/practical-c

6a95c60a340539eee82192c2d7934a71dfc4e6a5 — Bobby Hamblin a year ago e4a8790 master
excercises through advanced pointers
A airline_system/Makefile => airline_system/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for airline_system

# Turn on debugging
$CFLAGS=-g
airline_system:airline_system.o
	$(CC) $(CFLAGS) -o airline_system airline_system.o

A airline_system/airline_system.c => airline_system/airline_system.c +49 -0
@@ 0,0 1,49 @@
/****************************************
* airline_system - airline reservation	*
*   system				*
* Usage: Enter two airport codes at	*
*   prompt				*
****************************************/
#include <stdio.h>
#include <string.h>

#define MAX_ENTRIES 50

int main(void) {
	char line[100];	/* user input line */
	int counter = 0; /* for loop counter */
	char search_originating_code[3];
	char search_destination_code[3];

	struct airline_index {
		int flight_number;	
		char originating_code[3];	/* three character departing abbreviation */
		char destination_code[3];
		char departure_time[5];		/* in the format XX:XX */
		char arrival_time[5];
	} reservation_data = {
		8025,
		"PDX",
		"DFW",
		1845,
		2250
	};

	/* Receive user input */
	for (counter = 0; counter < MAX_ENTRIES; counter++) {
		printf("Originating airport code (e.g. PDX): ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", search_originating_code);

		if (strcmp(search_originating_code, "") == 0)
			break;
	}

	for (counter = 0; counter < MAX_ENTRIES; counter++) {
		if (strcmp(reservation_data[counter].originating_code, search_originating_code) == 0)
			printf("%d, %s, %s, %s, %s", reservation_data[counter].flight_number, reservation_data[counter].originating_code, reservation_data[counter].destination_code, reservation_data[counter].departure_time, reservation_data[counter].arrival_time); 
	}

	return (0);
}


A count/Makefile => count/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for the program count

# Turn on debugging
$CFLAGS=-g
count:count.o
	$(CC) $(CFLAGS) -o count count.o

A count/count.c => count/count.c +37 -0
@@ 0,0 1,37 @@
/****************************************
* count -- count number of times value	*
*   appears in array			*
****************************************/
#include <stdio.h>

/****************************************
* count -- count values in array	*
* Parameters				*
*   number -- number to count		*
*   array -- array to be searched	*
*   length -- length of array		*
* Returns				*
*   instances -- number of times value	*
*     appears in array			*
****************************************/
int count(int number, int array[], int length) {
	if (array[0] == 0)
		return (0);

	if (array[0] == number)
		return 1 + count(number, array + 1, length - 1);
	/* else */
	return count(number, array+1, length - 1);
}

int main(void) {
	int array[] = {1, 2, 3, 4, 3, 2, 1};
	int length = sizeof(array) / sizeof(array[0]);
	int number = 3;

	int frequency = count(number, array, length);

	printf("The number %d appears %d times in the array.\n", number, frequency);

	return (0);
}

A lc/Makefile => lc/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for the program lc

# Turn on debugging
CFLAGS=-g
lc:lc.o
	$(CC) $(CFLAGS) -o lc lc.o

A lc/lc.c => lc/lc.c +59 -0
@@ 0,0 1,59 @@
/****************************************
* lc -- count lines in a file		*
* Usage: Input filename at prompt	*
****************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* exit function */

/****************************************
* line_count -- count lines in a file	*
* Parameter				*
*   filename -- file to be counted	*
* Returns				*
*   lc -- integer number of lines in	*
*	  file string			*
****************************************/
int line_count(char filename[]) {
	FILE *input_file; /* input file */
	int lc = 0; /* line count */
	int ch = 0; /* character or EOF flag from input */

	input_file = fopen(filename, "r");

	if (input_file == NULL) {
		printf("Can not open file: %s\n", filename);
		exit(8);
	}

	while (1) {
		ch = fgetc(input_file);

		/* stop counting at EOF */
		if (ch == EOF)
			break;

		/* increment lc on new line */
		if (ch == '\n')
			lc++;
	}

	return lc;
}

int main(void) {
	char line[100]; /* input buffer */
	char filename[100]; /* input file name */

	printf("Enter a filename: ");
	fgets(line, sizeof(line), stdin);
	sscanf(line, "%s", filename);

	/* Count words in text */
	int lc = line_count(filename);

	printf("Line count: %d\n", lc);

	return (0);
}


A lc/test.txt => lc/test.txt +5 -0
@@ 0,0 1,5 @@
test
test
test
test
test

A mailing_labels/Makefile => mailing_labels/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for mailing_labels

# Turn on debugging
$CFLAGS=-g
mailing_labels:mailing_labels.o
	$(CC) $(CFLAGS) -o mailing_labels mailing_labels.o

A mailing_labels/mailing_labels.c => mailing_labels/mailing_labels.c +60 -0
@@ 0,0 1,60 @@
/****************************************
* mailing_labels - generate mailing	*
*   labels from names and addresses	*
* Usage: Input name and address at	*
*   prompt, entering a blank line to	*
*   complete list			*
****************************************/
#include <stdio.h>
#include <string.h>

#define MAX_ENTRIES 50

int main(void) {
	char line[100];	/* user input line */
	int counter = 0; /* for loop counter */

	struct mailing {
		char name[30]; /* name of person */
		char address1[60]; /* Two lines of street address */
		char address2[60];
		char city[40];
		char state[2];	/* two character abbreviation */
		int zip;	/* numeric zip code */
	};

	struct mailing list[MAX_ENTRIES];

	/* Receive user input */
	for (counter = 0; counter < MAX_ENTRIES; counter++) {
		printf("Name: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", list[counter].name);

		if (strcmp(list[counter].name, "") == 0)
			break;

		printf("Address first line: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", list[counter].address1);

		printf("Address second line: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", list[counter].address2);

		printf("City: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", list[counter].city);

		printf("State (e.g. OR): ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%s", list[counter].state);

		printf("Zip code: ");
		fgets(line, sizeof(line), stdin);
		sscanf(line, "%d", &list[counter].zip);
	}

	return (0);
}


A point_first_char/Makefile => point_first_char/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for point_first_char

# Turn on debugging
CFLAGS=-g
point_first_char:point_first_char.o
	$(CC) $(CFLAGS) -o point_first_char point_first_char.o

A point_first_char/point_first_char.c => point_first_char/point_first_char.c +35 -0
@@ 0,0 1,35 @@
/****************************************
* point_first_char -- return pointer to	*
*   first nonwhite character in string	*
* Usage: Enter string at prompt		*
****************************************/
#include <stdio.h>
#define MAX 10

void first_nonwhitespace(char *str_ptr) {
	int index; /* counts which array element is being operated on */

	for (index = 0; *str_ptr != '\0' ; index++) {
		break;
	}
	str_ptr++;
}

int main(void) {
	char line[100]; /* user input buffer */
	char str[100]; /* string input by user */

	/* array containing data to zero */
	printf("Input string: ");
	fgets(line, sizeof(line), stdin);
	sscanf(line, "%s", str);

	/* call function with a pointer to first element of array */
	first_nonwhitespace(str);

	printf("%s\n", str);

	return (0);
}



A snake_case/Makefile => snake_case/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for snake_case

# Turn on debugging
$CFLAGS=-g
snake_case:snake_case.o
	$(CC) $(CFLAGS) -o snake_case snake_case.o

A snake_case/snake_case.c => snake_case/snake_case.c +45 -0
@@ 0,0 1,45 @@
/****************************************
* snake_case - convert text from kebab-	*
*   case to snake_case			*
* Usage: Input string at prompt		*
****************************************/
#include <stdio.h>
#include <string.h>

int main(void) {
	char kebab_case[100]; /* Text before conversion */
	char convert_snake_case(char kebab_case[]); /* Convert snake case function prototype */

	/* Receive user input */
	printf("Input text: ");
	fgets(kebab_case, sizeof(kebab_case), stdin);

	convert_snake_case(kebab_case);
	/* Print result */
	printf("%s", kebab_case);
	return (0);
}

/****************************************
* convert_snake_case -- replace all	*
*   occurrences of '-' with '_'		*
* Parameter				*
*   kebab_case -- text to be converted	*
* Returns				*
*   snake_case -- converted text	*
****************************************/
char convert_snake_case(char kebab_case[]) {
	int counter = 0;
	char snake_case[100];

	for (counter = 0; kebab_case[counter] != '\0'; counter++) {
		if (kebab_case[counter] == '-')
			snake_case[counter] = '_';
		else
			snake_case[counter] = kebab_case[counter];
	}

	strcpy(kebab_case, snake_case);

	return snake_case[100];
}

A wc/Makefile => wc/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for the program wc

# Turn on debugging
CFLAGS=-g
wc:wc.o
	$(CC) $(CFLAGS) -o wc wc.o

A wc/wc.c => wc/wc.c +42 -0
@@ 0,0 1,42 @@
/****************************************
* wc -- simple word counter		*
* Usage: Input string at prompt		*
* Notes: A "word" is defined as a	*
*   collection of characters separated	*
*   by a space or hyphen.		*
****************************************/
#include <stdio.h>
#include <string.h>

/****************************************
* word_count -- count words in a string	*
* Parameter				*
*   text -- string to be counted	*
* Returns				*
*   wc -- integer count of words in	*
*	  string			*
****************************************/
int word_count(char text[]) {
	int wc = 0;
	int counter = 0;

	for (counter = 0; text[counter] != '\0'; counter ++) {
		if (text[counter] == ' ')
		wc++;
	}

	return wc;
}

int main(void) {
	char text[100];

	/* Count words in text */
	int word_count(char text[]);

	printf("Enter a sentence: ");
	fgets(text, sizeof(text), stdin);

	printf("%d", word_count(text[100]));
	return (0);
}

A xref/Makefile => xref/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for program xref

# Turn on debugging
$CFLAGS=-g
xref:xref.o
	$(CC) $(CFLAGS) -o xref xref.o

A zero_array/Makefile => zero_array/Makefile +6 -0
@@ 0,0 1,6 @@
# Makefile for zero_array

# Turn on debugging
$CFLAGS=-g
zero_array:zero_array.o
	$(CC) $(CFLAGS) -o zero_array zero_array.o

A zero_array/zero_array.c => zero_array/zero_array.c +37 -0
@@ 0,0 1,37 @@
/****************************************
* point_first_char -- return pointer to *
*   first nonwhite character in string   *
* Usage: Enter string at prompt         *
****************************************/
#include <stdio.h>
#define MAX 10

void first_nonwhitespace(char *str_ptr) {
    int index; /* counts which array element is being operated on */

    for (index = 0; index < MAX; index++) {
        *str_ptr = "test";
        break;
    }
}

int main(void) {
    char line[100]; /* user input buffer */
    char str[100]; /* string input by user */
    char *str_ptr; /* pointer to string */

    /* array containing data to zero */
    printf("Input string: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%s", str);

    /* assign address of str to str_ptr */
    str_ptr = str;

    /* call function with a pointer to first element of array */
    first_nonwhitespace(str_ptr);

    printf("%s", str_ptr);

    return (0);
}