Starting from:

$30

COMP1410-Lab 7 : Standard Library Functions and C Structures Solved

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 );

3.       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: 

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

 

Inside of main()
2)       Declare an array named WordList of type myWord with size 20.

 

3)       Declare a string as follows:

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

fox";
 

4)       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). 

 

5)       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]. 

 

6)       Print the contents of the WordList.

 

7)       Sort the WordList in alphabetical order.  (Hint: use a simple bubble sort algorithm and use the library function strcmp() to compare two words.) 

 

8)       Print the contents of the now sorted WordList.

More products