Introduction to Computer Science I by James Tam | Return to the course web page |
A string is a collection of characters that include a set of built in operations. Common string functions include:
You are to implement the first two string functions for this assignment. The string or strings must be implemented as a one-dimensional character array (for feature #2 you will need two character arrays). You cannot use the 'string' type nor can you use the string functions that are provided with the string type.
First character | Second character | Third character | Fourth character |
'a' (ASCII 97) | 'b' (ASCII 98) | 'c' (ASCII 99) | EOL/SPACE (ASCII 32) |
|
|||||
|
|||||
|
Array element[1] | Array element[2] | Array element [3] | Array element[4] | Array element [5] | ... | Array element [81] |
'a' | 'b' | 'c' | EOL/SPACE | Not occupied | ... | Not occupied |
When your program displays the array onscreen it should display all the characters except for the character containing the EOL/SPACE (to do this your program must explicitly check for and avoid displaying this value) Thus the EOL/SPACE character is used to mark the end of the input. You can assume that the strings entered by the user will not explicitly contain a space (and it is acceptable if your program treats any spaces entered by the user as the end of input so it won't read in any values that follow the space). Finally you must implement another procedure that will display the integer ASCII value for each character element in the string up to and including the EOL/SPACE so in the previous example the program would display 97, 98, 99, 32. Note: The display of the ASCII values not a function that is typically implemented when writing a string but it is a useful debugging tool for you and it allows your TA to mark your assignment more quickly. If you need to see the table of ASCII values at the UNIX command line type 'man ascii'.
Missing the above features:
Each of the extra features below are worth an additional two letter 'steps' (e.g., 'C' to 'B-'). These features can be implemented in whatever combination that you wish. Implementing the base assignment and all three extra features makes you eligible to receive an 'A' grade. The other submission requirements (shown below) and coding style requirements must also be fulfilled.
1) | Checking the input: Your program must check that the number of characters that will be stored in the array doesn't exceed the size of the array. If the user types in more characters than can be stored in the array your program should ignore any additional characters after the 80th and also it must mark the end of the array by putting the end-of-line marker in the 81st element. (JT's hint: When testing if this feature works you may want to reduce the size of the array from 80 to a more manageable size like 6 so you don't have to keep typing 81 plus characters each time that want to run a test). | |
Before you can get credit for the features below your program must run in an iterative fashion (loop until the user quits the program in the same way that the sample executable works) and it must display a menu of functions. Two of the menu functions will correspond to the string functions below (as you implement them) and the third will allow the user to quit the program. If you implement one or both of the features below but miss the requirement for looping the program and displaying the menu then your program will lose a letter step. | ||
2) | Implementing a string length feature: This feature will take the form a function that takes as input a string and it returns an integer value that corresponds to the length of the string. This excludes the marker for the end-of-line which means that if the user simply hits enter when prompted to type in a string then the function will count the length of the string as zero. Again your program should display the ASCII values for the string that was stored in memory. After determining the length of the string your program should display onscreen the characters in the string and it's length. | |
3) | Implementing a string compare feature: When this feature is selected from the main menu will prompt the user to enter in two strings. This feature will take the form of a function that takes as input the two strings to be compared and returns one of three integer values depending upon the result of an alphabetical comparison of the two strings: -1 if the first string comes before the second, +1 if the first string comes after the second string or 0 if the strings are identical. Your program will then display onscreen both strings as well as the ordering of both strings. Finally your program should display the ASCII values for both strings. Note: You must do the comparison by manually checking each element of the character arrays yourself. You won't get credit if you simply use a logical operator like '<' to compare the two arrays (e.g., if (array1 < array2) then...) because you are supposed to figure out how to do it yourself. |
1 What does and doesn't constitute a sufficient amount of time and effort? It's a judgment call on the part of your marker. More often than not if you put in a reasonable amount of effort into your assignment and for some reason you just couldn't get it to work then you will receive some credit for your work. An example of when you wouldn't receive credit is when you simply handed someone else's work. This latter case assumes that you properly cited the other person's work, if you didn't cite your source and tried to claim that it was your own work then it would be an example of academic misconduct (cheating).