CPSC170
Fundamentals of Computer Science II

Lab 10

Type casting and working with strings (arrays of characters)


  1. In the pre-lab reading you saw, and studied, the program in the file chars.cc. The program read characters from the file until the end of file character was read. Note that the end of file character is different from the end of line character. Also, note that when a program is reading input typed by the user interactively at the terminal, the keystroke control-d indicates end of file. When you type your input into a text file using a text editor (such as emacs), the editor adds an end of file character at the end of whatever was typed into the file. Thus, if you type the characters:

    abcd<ret>
    and then saved the file, the program in chars.cc should read (and print out) five characters and then stop. file1.txt is a file typed this way. Try the program in chars.cc redirecting input to the program from the file file1.txt with the command (assuming the executable for the program is called chars)
    ./chars < file1.txt

  2. On the other hand, if the input file were typed as

    abcd

    and then saved, then there is no new line character in the file, and the end of file character is placed as the character after the character 'd'. In this case, the program in chars.cc should read (and print out) four characters and then stop. file2.txt is a file typed this way. Try the program in chars.cc redirecting input to the program from the file file2.txt.

  3. Write a program that reads in one line of characters, that contain only letters and digits (alpha-numeric characters), from the user and outputs the number of numbers that the user entered. For example, if the user input was

    ab10cd5e6g9aa15
    then your program should output
    You entered 5 numbers.

    You may assume that the line will not have any more than 50 characters. The end of the line of characters is indicated by the end of line character or by the end of file character; your program should work in either case.

    Create suitable test cases for your program.

    Copy the file countNumbers.cc, and study the function main. Complete the function main, and create the necessary .h and .cc files. Add suitable targets to your Makefile, compile your program and run the executable on your test cases to verify that your program runs as expected.

  4. Write a program, with executable name numberwords, that reads in a file (using input redirection) containing letters, spaces and newline characters, and output the number of words in the input. Words are defined as sequences of letters without spaces and newline characters. For example, if text1.txt is the input file, then your program should output:

    The input contains 7 words.
    As usual, first create files that you can use as good, exhaustive test cases.

    You may not assume any upper bound on the number of characters on a line, or in a word. The end of file character may be at the end of a line of characters or as the first character on a new line; your program should work in either case.

    Is it necessary for the program to store the words in arrays of characters? If so, make sure to add the end of string character at the end of each word, and also make sure that there is space allocated in the array for this end of string character.

  5. Modify your program numberWords to create a new executable words_before_end that reads in a file of characters with the same conditions as above, and prints out the number of words that appear before the word end (all lowercase) appears in the file. If the word end does not appear in the file, then the total number of words in the file should be printed. For example,

    ./words_before_end < text1.txt 
    should have the output
    There are 7 words before end.

    If text2.txt is given as input to your program, the output should be:

    There are 2 words before end.

    You may assume that words are no more than 10 characters long. As before, the end of file character may be at the end of a line of characters or as the first character on a new line; your program should work in either case.

    Is it necessary for the program to store the words in arrays of characters? If so, make sure to add the end of string character at the end of each word, and also make sure that there is space allocated in the array for this end of string character.

  6. Modify the program that was started in the file countNumbers.cc and that you completed for Item 3 above so that the program also prints the string that, amongst all the strings that the user entered, appears first in dictionary ordering. For the example user input from Item 3 above, your program should print:

    You entered 5 numbers.
    The earliest string in dictionary ordering is: aa
    	
    You may assume that the strings entered by the user are all in lower case letters, and are no more than 6 characters long. There is no upper bound on how many strings and/or numbers the user could input. Any additional functions you write for this problem must be in a separate file than the functions you wrote for the previous problem. If you reuse some of the functions you wrote for the previous problem, do not include them in the new file of functions.

    Add suitable targets to your Makefile, compile your program and run the executable to verify that your program runs as expected.