c语言编程统计ReadMe.txt英文文本文件中,有多少个大写字母、小写字母、数字、空格、换行以及其他字符,输出统计结果。(ReadMe.txt请下载后放在项目文件夹中)
时间: 2024-05-07 19:20:58 浏览: 17
Sure. Here's the Python code to perform the required task:
```python
# Open the file in read-only mode
with open('ReadMe.txt', 'r') as file:
# Initialize the counters
uppercase_count = 0
lowercase_count = 0
digit_count = 0
space_count = 0
newline_count = 0
other_count = 0
# Loop over each line in the file
for line in file:
# Loop over each character in the line
for char in line:
# Check if the character is a uppercase letter
if char.isupper():
uppercase_count += 1
# Check if the character is a lowercase letter
elif char.islower():
lowercase_count += 1
# Check if the character is a digit
elif char.isdigit():
digit_count += 1
# Check if the character is a space
elif char.isspace():
space_count += 1
# Check if the character is a newline
elif char == '\n':
newline_count += 1
# Otherwise, the character is something else
else:
other_count += 1
# Print the results
print(f"Uppercase letters: {uppercase_count}")
print(f"Lowercase letters: {lowercase_count}")
print(f"Digits: {digit_count}")
print(f"Spaces: {space_count}")
print(f"Newlines: {newline_count}")
print(f"Other characters: {other_count}")
```
Assuming that the `ReadMe.txt` file is located in the same directory as the Python script, running the script will output the required statistics.