0% found this document useful (0 votes)
9 views4 pages

Program File Questions(Text File)

The document outlines four programming tasks involving file manipulation. The tasks include creating a word frequency counter, extracting error and warning messages from a log file, merging multiple text files with headers, and encrypting a text file with a shift cipher. Each task specifies the required functionality and provides example inputs and outputs.

Uploaded by

ridaayan7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Program File Questions(Text File)

The document outlines four programming tasks involving file manipulation. The tasks include creating a word frequency counter, extracting error and warning messages from a log file, merging multiple text files with headers, and encrypting a text file with a shift cipher. Each task specifies the required functionality and provides example inputs and outputs.

Uploaded by

ridaayan7860
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program File Questions (Text File)

Q1. WORD FREQUENCY COUNTER


Write a program that reads a text file named as city.txt and prints the 10 most
frequent words. Program should ignore words appearing once. Also handle the
case sensitivity. [Create the file also]

Eg:- If the file contains text as follows:

In the heart of the city, lights flicker as the sun sets behind the skyline.
People rush home from work, while others wander the streets.
Music pours from cafes and cars, blending with the hum of conversation.
The city never truly sleeps—it (CITY) only changes its rhythm.

Then output should be:

Word Frequency
the 5
city 2
of 2
from 2

Q2. LOG FILE ERROR AND WARNING EXTRACTOR

Write a program to extract all lines containing the word "ERROR" and
“WARNING” from a file logfile.txt and save them to another file err_war.txt.
Count how many warning and how many error messages are there .

Eg:- If the file logfile.txt contains text as follows:

[INFO] 2025-07-17 08:00:00 - System startup complete.


[DEBUG] 2025-07-17 08:01:12 - Checking user session validity.
[ERROR] 2025-07-17 08:01:45 - Failed to connect to database.
[WARNING] 2025-07-17 08:02:00 - Low memory warning.
[INFO] 2025-07-17 08:02:30 - Background tasks initialized.
[ERROR] 2025-07-17 08:03:15 - File not found: config.yaml
[DEBUG] 2025-07-17 08:03:45 - User login attempt: admin
[INFO] 2025-07-17 08:04:00 - Scheduled job started.
[ERROR] 2025-07-17 08:04:30 - Permission denied writing to /var/log/app.log
then err_war.txt should contain:

[ERROR] 2025-07-17 08:01:45 - Failed to connect to database.


[WARNING] 2025-07-17 08:02:00 - Low memory warning.
[ERROR] 2025-07-17 08:03:15 - File not found: config.yaml
[ERROR] 2025-07-17 08:04:30 - Permission denied writing to /var/log/app.log

There are 3 ERRORS and 1 WARNING in logfile.txt

Q3. FILE MERGER


Write a program that merges the contents of file1.txt, file2.txt, and file3.txt into
one output file merged.txt. Add headers before each file's content in the merged
file. Consider the situation if one of the files does not exist? [use exceptions]
Modify the program to ignore empty files when merging.

Eg: Sample text


file1.txt – Meeting Notes

vbnet
CopyEdit
Team meeting held on July 17.
Discussed project milestones and deadlines.
Action items assigned to Gaurav, Priya, and Chetna.

file2.txt – Error Logs

vbnet
CopyEdit
[ERROR] 2025-07-17 09:15 - Failed to authenticate user.
[ERROR] 2025-07-17 09:18 - Timeout while accessing remote server.

file3.txt – Daily Summary

pgsql
CopyEdit
All scheduled tasks completed.
System backup successful.
No incidents reported today.

file4.txt – (Missing or Empty)


File does not exist or is intentionally left blank.

Expected Merged and Output (merged.txt)

pgsql
CopyEdit
--- Content from file1.txt ---
Team meeting held on July 17.
Discussed project milestones and deadlines.
Action items assigned to John, Priya, and Chen.

--- Content from file2.txt ---


[ERROR] 2025-07-17 09:15 - Failed to authenticate user.
[ERROR] 2025-07-17 09:18 - Timeout while accessing remote server.

--- Content from file3.txt ---


All scheduled tasks completed.
System backup successful.
No incidents reported today.
________________________________________________________________

[If file4.txt is empty or missing, it is safely skipped with a printed message:

Skipping missing file: file4.txt ]

Q4. ENCRYPTOR

Write a program to encrypt a text file original.txt with a shift of 5 into another
file named as encrypt.txt. Numbers and special symbols remain unchanged.

Sample Text in original.txt

Confidential Report: Q3 Financial Summary

The third quarter showed a significant increase in net revenue, totaling $4.3
million.

Operational costs remained stable, while marketing expenses saw a slight uptick.
Please ensure this report is distributed only to authorized personnel.

Prepared by: Finance Department


Date: July 17, 2025

Encypted text in encrypt.txt


Htsknijsjfy Qjwtwy: V3 Knsjhfwqn Xzrrfd

Ymj ymnwi vzfyyjw xmtbji f xnlskwfynts nsjwjfhj ns sjy wjajzaj, ytqfqnsl $4.3
rnqqtws.

Tujwfwfynts rtyxy wjfrfns ijgqqj, bnymf rfwlznytl jruujxj xbf f xqnlmy zyuphp.
Uqjfxj jszwzx ymnx wjtwfy nx ixynuywijsy tsqd yt fzwtwduyz ujwxfqj.

Uwtujwfy gd: Knfsjhj Ijrujwqjtsy


Ifyj: Ozqd 17, 2025

You might also like