Lab – 7 Standard Library Functions and C Structures
60-141 – Introduction to Programming II Winter, 2017
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:
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.
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.
Answer:
Part A
/* Title: Lab #7: Standard Library Function and C Structure Objective: To define and test some functions including AtoI, StrCmp, StrCpy, StrCat and StrChr. */ #include<stdio.h> #include<stdlib.h> //Function Prototypes 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 ); int main(){ //Declarations int choice, stringCompare; char character, source[256], destination[256]; //Calling the AtoI function printf("Please enter a numeric string: "); scanf(" %[^\n]", source); printf("%s to integer is %d\n", source, AtoI(source)); //Calling the StrCmp function printf("Please enter string #1 to compare: "); scanf(" %[^\n]", source); printf("Please enter string #2 to compare: "); scanf(" %[^\n]", destination); stringCompare = StrCmp(source, destination); if(stringCompare > 0) printf("String #1 is greater than String #2\n"); else if(stringCompare < 0) printf("String #2 is greater than String #1\n"); else printf("String 1 and 2 are equal\n"); //Calling the StrCpy function printf("Please enter a string to copy: "); scanf(" %[^\n]", source); StrCpy(destination, source); printf("Your original string is: %s\n", source); printf("And the Copied string is: %s\n", destination); //Calling the StrCat function printf("Please enter string #1: "); scanf(" %[^\n]", destination); printf("Please enter string #2 to concatenate to string #1 : "); scanf(" %[^\n]", source); StrCat(destination, source); printf("String #1 + String #2: %s\n", destination); //Calling the StrChr function printf("Please enter a string you want to search a character: "); scanf(" %[^\n]", source); printf("Please enter a character to search: "); scanf(" %c", &character); printf("Character %c %s in the string %s\n", character, StrChr(source, character)?"is":"is not", source); } /* Objective: Convert a string to an integer input: A string output: Return an integer */ int AtoI ( const char * str ){ int i = 0; int j = 0; while(str[i] != '\0') { if(str[i] == '.') break; if(str[i] >= '0' && str[i] <= '9') j = (j*10) + str[i] - '0'; i++; } return j; } /* Objective: Compare the length of two strings input: str1 and str2 are pointers to different, non-overlapping, collections of characters (char). output: Returns an integer value (int) indicating: 1 :: str1 > str2; 0 :: str1 == str2; -1 :: str1 < str2. */ int StrCmp ( const char * str1, const char * str2 ){ int i = 0; while(str1[i] != '\0' && str2[i] != '\0'){ if(str1[i] > str2[i]) return 1; if(str1[i] < str2[i]) return -1; i++; } if(str2[i] == '\0') return 1; if(str1[i] == '\0') return -1; return 0; } /* Objective: copies the string pointed to, by source to destination. input: pointer to the destination array where the content is to be copied and the string to be copied (source) output: Returns a pointer to the destination string. */ char * StrCpy ( char * destination, const char * source ){ int i = 0; while(source[i] != '\0'){ destination[i] = source[i]; i++; } destination[i] = '\0'; return destination; } /* Objective: To appends the string pointed to by source to the end of the string pointed to by destination. input: Destination which is pointer to the destination array, which should contain a C string and a source which is the string to be appended output: This function returns a pointer to the resulting string destination. */ char * StrCat ( char * destination, const char * source ){ int i = 0, j = 0; while(destination[i] != '\0') i++; while(source[j] != '\0'){ destination[i] = source[j]; i++; j++; } destination[i] = '\0'; return destination; } /* Objective: searches for the first occurrence of the character in the string. input: str which is the string to be scanned and the character to be searched in str. output: This returns a pointer to the first occurrence of the character in the string or NULL if the character is not found. */ char * StrChr ( char * str, int character ){ while( (*str != '\0') && (*str != (char)character) ) str++ ; // Search for C if( *str != '\0' ) // Did not find if delimiter located return str ; else return NULL ; }
Part B
/* Title: Lab #7: Standard Library Function and C Structure Objective: To define and test some functions including AtoI, StrCmp, StrCpy, StrCat and StrChr. */ #include<stdio.h> #include<stdlib.h> #include<string.h> //Define a C structure "myWord" struct myWord{ char Word[21]; int Length; }; int main() { int i, j, n = 0; //Declare an array WordList of type myWord with size 20 struct myWord wordList[20],temp, *wordListPtr; char *tokens; //Declare a string "myString" char myString[] = "the cat in the hat jumped over the lazy fox"; //char myString[] = "you will be evaluated based on your solutions for the problems based on the following scheme"; //char myString[] = " "; //Use library function strtok() to extract each word from "myString" wordListPtr = wordList; tokens = strtok(myString, " "); //Use the library function strlen() to find the length of each word and store the value in the member length while (tokens != NULL) { strcpy(wordList[n].Word, tokens); (wordListPtr[n].Length = strlen(tokens)); n++; tokens = strtok(NULL, " "); } //Print the contents of the WordList printf("The content of the wordList before sorting\n"); for(i = 0; i < n; i++) printf("%-15s\t%d\n", wordListPtr[i].Word, wordListPtr[i].Length); //Sort the WordList in alphabetical order(using bubble sort algorithm and use the library function strcmp() to compare two words) for(i = 0; i < n; i++) { for(j = 0; j < n - 1; j++) { if(strcmp(wordListPtr[j].Word, wordListPtr[j+1].Word) > 0) { temp = *(wordListPtr+j); *(wordListPtr+j) = *(wordListPtr+j+1); *(wordListPtr+j+1) = temp; } } } printf("\nThe content of the wordList after sorting\n"); for(i = 0; i < n; i++) printf("%-15s\t%d\n", wordListPtr[i].Word, (wordListPtr[i].Length)); return 0; }
Leave a reply