0% found this document useful (0 votes)
2 views

CS170 File String

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS170 File String

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab: File String

In this lab, we would like to process the input string values that we read from files. In addition, we
would like to store the processed results of a program by writing the contents to a file.

Purpose:
Reading input from a file and writing the processed results to a file are immensely helpful. In
addition, processing the input text values by using string library functions are something that we are
interested to understand, practice, and learn in this assignment.

Problem 1: Count digits of a file [1 program + 1 screenshot]


Lab Problem: [save the program as: countDigits.py]
In this problem, we would like to read from a text file and count how many digits we have in the file.
The program should display the count at the end. You can modify the following function and use it in
your program. The following program, reads a line of text and uses the countChars function to
determine how many lowercase characters we have in the line of text.

# this function determines total number of


# lowercase characters in the text
def countChars(line):
count = 0
for ch in line:
if(ch.islower()):
count = count + 1
return count

# we input a line of text.


# the function returns the count of lower case characters in the line of text
sentence = input("Enter a line of text: ")
totalLowerCaseChars = countChars(sentence)
print("You have entered", totalLowerCaseChars, "lower case characters")

>> Deliverable 1: Modify the function countChars to count number of digits in the line of text. Use
the function and read from a file one line at a time and count the total number of digits that we have
in the file. The input file for this program is named "wordNumbers.txt".

Sample Input (assume the content of the wordNumbers.txt file is the following:)
The word "hundred" is actually derived from the Old Norse word "hundrath,"
which actually means 120, not 100.
More specifically, "hundrath," in Old Norse, means "long hundred,"
which equals 120, due to the duodecimal system.
But good luck trying to argue that your $100 bill is worth 20 percent more
than it is.

Sample Output (the following output will be written in the areaPerimeter.txt file)
The file has 14 digits
Problem 2: All uppercases [1 program + 1 screenshot]
Lab Problem: [save the program as: convertUpperCase.py]
In this program, read line of texts from an input file and convert the line of texts into uppercases and
save it in an output file. We have demonstrated a function called convertCase that takes a line of text
as a parameter and returns the it in all lowercases. You can modify this function and use it to read
from an input file named "lowercases.txt" and create an output file named "uppercases.txt".

# this function takes a line of text and converts it


# into lowercases
def convertCase(line):
return line.lower()

# we input a line of text and the funciton converts the


# line of text into all lowercases
sentence = input("Enter a line of text: ")
lowercaseSentence = convertCase(sentence)
print("Converted into lowercase: ", lowercaseSentence)

>> Deliverable 2: You can take advantage of the function and modify it for this program. Read line
of texts from an input file one line at a time. After reading the line, convert it into all uppercase.
Further, write the line to an output file.

Sample Input (assume the content of the lowercases.txt file is the following:)
I can't change the direction of the wind,
but I can adjust my sails to always reach my destination.

Sample Output (the following output will be written in the uppercases.txt file)
I CAN'T CHANGE THE DIRECTION OF THE WIND,
BUT I CAN ADJUST MY SAILS TO ALWAYS REACH MY DESTINATION.

You may assume there are no errors in the input. As previously noted, please use only the features of
the language taught in the class. Please let me know if you have any further questions.

The deadline to submit the lab is: 11:59pm, Monday 15 July. Please let me know if you
have any further questions. Thank you.
Rubrics
When you are satisfied with your work, submit all three of the Python program files
(convertUpperCase.py, countDigits.py) and 2 screenshots on the Blackboard course website.

Criterion Details Deductions


-5: For each missing file
[countDigits.py]
-5: the program does not read from
the file properly
-3: Does not use a function
-5: the program does not
determine the correct number of
digits in the input file
Program file
convertUpperCase.py, countDigits.py
Requirement
[convertUpperCase.py]
-5: the program does not use a
function to convert the lines of text
into upper cases
-3: the program does not read
inputs from the file
-3: the program does not write
outputs to a file

-5: comments were not used


throughout the program

Comments have been used throughout the


-5: separate files were not
Program program to identify the author of the
created for each Python
requirement program and explain the steps of the
programs
program
-10: used programming features
that have not been taught in the
class
Screenshot Submit screenshots from deliverable 1, -3: for each missing screenshots
deliverable 2 in order to show sample runs
of the programs
Test Note, whether the programs compile and -5: the code does not compile
run or run

You might also like