CPE 101
Spring 2006
Laboratory 10

Due Date

 

 

Objectives

 

 

Requirements

This lab is for practice using the C string functions strcpy, strncpy, strcat, strncat, strcmp, strlen.  You will use these functions to write some practice code, to write some other helpful string functions, and to do some of the tasks that will be necessary for your next programming assignment.

 

1.  Read in two strings from the keyboard, and compare them to see if they're the same.  Repeat until the first string is "stop".

 

2.  Read in strings from the keyboard and concatenate them into one long string.  After each string is read in, print the long string so far.  Stop when the input string is "stop".

 

3.  Write a function that takes a string in the form "month-day-year" and returns a new string "year,month,day".  Read in from the keyboard the string to convert and pass it into the function.  The returned string should be returned in an input-output parameter.

 

4.  Write a function char  charAt(char str[ ], int) that takes a string and an integer, and returns the character at that location in the string.  If the location is beyond the end of the string, or less than 0, return null ('\0').  Test by reading in a string from the keyboard and repeatedly reading in integer values to pass into the function.  Stop when the integer value is -1.

 

5.  Write a function int  indexOf(char str[ ], char ch) that finds the first location of ch in str.  For instance indexOf("this is a test", 's') would return 3.  If the character does not appear in the string return -1.  Test by reading in a string from the keyboard and repeatedly reading in characters.  Stop when the character entered is 'z'.

 

6.  Write a function int  find(char str1[ ], char str2[ ]) that takes two strings and returns the first location of str2 in str1.  For instance, find("this a test is", "is") would return 2.  If str2 is not contained in str1, return -1.  Read in pairs of strings from the keyboard and print the results until the first string is "stop".

 

7.  Enhance the function you just wrote by adding another int parameter that tells the function where to start looking in str1.  Call this function findE.  As an example, findE("this a test is", "is", 1) would return 2, and findE("this a test is", "is", 4) would return 12.  Test by reading in a pair of strings and repeatedly reading in integers.  Call findE( ) each time with the same strings but the new integer until the integer read in is -1.

 

8.  Write a function void  concat(char str[ ], char ch, char newStr[ ]) that concatenates ch onto the end of str and returns the result in newStr.  For example, concatenating "help" and 'm' would be "helpm".  Test by reading in a string and repeatedly reading in characters, concatenating each onto the end of the previous string until the character entered is 'z'.

 

9.  Write a function to initialize a Boggle board, as described in the assignment 6 writeup.  The board should consist of 9 rows, each of 9 random upper-case letters (plus the terminating character for a string).  The declaration should look like char board[9][10].  Declare the board to be a two dimensional array of characters 9x10 (nine rows of 10 characters).  Initialize the array using a double-nested loop to assign each character.  To access the character at row i, column j use board[i][j].  Print out the board using a double-nested loop.

 

10.  Now assign an array of string pointers to the rows of the Boggle board so that the rows can be treated as strings instead of individual characters.  To do this, declare an array of char pointers:  char *strs[9].  Now assign each string pointer to a row of characters in board using the following statement in a loop: strs[i] = board[i];  Once this is done, each slot of strs points to a string of characters (which is the same as a row of the two-dimensional array).  Now you can call string functions on the rows of characters.  Print out the grid using printf("%s\n", strs[i]); inside a loop.