COMP1410 Lab 7-Standard Library Functions and C Structures Solved

35.00 $

Category:

Description

5/5 - (2 votes)

Objective: (a) Learn more about the functions from the standard libraries.  There are a great many standard functions that are included with C compilers. These functions are designed to carry out various common and repeatedly performed tasks by the C programmers. Having a good understanding about these functions and properly utilizing them can greatly reduce the coding time for large and complex programs. (b) In C, a structure (struct) is an aggregate data type that allows us to group one or more variables of different data type together to form a new, more complex data type. In this lab we will practice some simple usage of structure along with some more practice using strings.

Work to do:

  1. Go to the web page: http://www.cplusplus.com/reference/clibrary/. Click on various C libraries and study the functions, including what are their purposes, type of their return values, the parameter lists, examples of uses, etc. In particular, review the functions in the following headers: <ctype.h>, <math.h>, stdio.h>, <stdlib.h>, <string.h> and <time.h>.
  2. In a simple way, define your own versions of the flowing functions. Test your functions by writing a main(). (Call the program Lab7a.c) Do not forget to document assumptions for each function that you program.

int AtoI ( const char * str ); int StrCmp ( const char * str1, const char * str2 ); char * StrCpy ( char * destination, const char * source ); char * StrCat ( char * destination, const char * source ); char * StrChr ( char * str, int character );

  1. Write a C program called “Lab7b.c” to accomplish the following:

Outside of main()

1) Define a C structure named myWord with the following members:

  1. char Word[21] ; b. int Length ;

Inside of main()

  • Declare an array named WordList of type myWord with size 20.

 

  • Declare a string as follows:

char myString[] = “the cat in the hat jumped over the lazy

fox”;

  • Use library function strtok() to extract each word from myString and store it in the WordList. (Assume space is the only delimiter separating the words).

 

  • Use the library function strlen() to find the length of each word and store the value in the member Length.

 

Note: To access the Word array inside WordList use the “dot” operator.  e.g. strcpy(WordList[n].Word, “hello”);

[If you prefer, you could maintain an integer variable to keep a record of the number of words extracted].

  • Print the contents of the WordList.
  • Sort the WordList in alphabetical order. (Hint: use a simple bubble sort algorithm and use the library function strcmp() to compare two words.)
  • Print the contents of the now sorted WordList.

Note: you should test your program with different myString values, including an empty string.

Summary of the lab requirements:  You must write two programs for this lab, namely Lab7a.c (for step 2) and Lab7b.c (for step 3).  Be prepared to demonstrate both programs with appropriate test input examples.

  • lab07-fcrsru.zip