Write a Program That Reads a Text File and Counts the Vowels in the File.

This is a Python Program to count the occurrences of a letter of the alphabet in a text file.

Trouble Description

The program takes a letter from the user and counts the number of occurrences of that letter in a file.

Problem Solution

1. Take the file proper noun and the letter to be counted from the user.
2. Read each line from the file and split the line to form a listing of words.
iii. Apply a for loop to traverse through the words in the list and another for loop to traverse through the letters in the word.
4. Check if the letter provided past the user and the letter encountered over iteration is equal and if they are, increment the letter count.
v. Exit.

Plan/Source Code

Here is source lawmaking of the Python Program to count the occurrences of a letter in a text file. The program output is also shown below.

fname              =              input              (              "Enter file proper noun: "              )              l=              input              (              "Enter letter to exist searched:"              )              k              =              0              with              open              (fname,              'r'              )              every bit              f:              for              line              in              f:         words              =              line.separate              (              )              for              i              in              words:              for              letter              in              i:              if              (letter of the alphabet==l):                     chiliad=k+1              print              (              "Occurrences of the letter:"              )              print              (k)            

Program Caption

one. User must enter a file name and the alphabetic character to be searched.
two. The file is opened using the open() role in the read mode.
3. A for loop is used to read through each line in the file.
four. Each line is separate into a list of words using separate().
5. A for loop is used to traverse through the words list and another for loop is used to traverse through the letters in the word.
six. If the alphabetic character provided past the user and the letter encountered over iteration are equal, the letter count is incremented.
vii. The final count of occurrences of the letter is printed.

Runtime Test Cases

  Case ane: Contents of file:  how-do-you-do world hello how-do-you-do   Output:  Enter file name: out.txt Enter letter to be searched:o Occurrences of the alphabetic character: 5   Case 2: Contents of file:  how-do-you-do world   examination test test   Output:  Enter file name: out1.txt Enter letter of the alphabet to be searched:e Occurrences of the letter: vi

Sanfoundry Global Educational activity & Learning Series – Python Programs.

To do all Python programs, hither is complete set of 150+ Python Problems and Solutions.

Manish Bhojasia - Founder & CTO at Sanfoundry

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

sousahisamoss.blogspot.com

Source: https://www.sanfoundry.com/python-program-read-file-counts-number/

Belum ada Komentar untuk "Write a Program That Reads a Text File and Counts the Vowels in the File."

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel