You are on page 1of 8

Problems & Solutions

Problem 1
Write a program to count the characters, words and lines from a text file

Script Name:: Count_in_txtfile


History::
What:: counting a words, lines, characters in file

Program :
#variable declaration
line=0
char=0
word=1
#opening file and read a file
with open('C:/Users/rrt11/Desktop/linux.txt','r')as file:

#reading every line in file


for i in file:
line=line+1
#reading every characters in a line
for j in i:
char=char+1
#reading words in line
if(j==' '):
word=word+1

print("the total lines is",line)


print("the total words in file is",word)
print("the total characters is in file",char)

Output:

the total lines is :- 11


the total words in file is :- 49
the total characters is in file:- 379
Problem 2

Script Name:: Capitalletter_in_txtfile


History::
What:: Capitalize first letter in each word

Write a program to capitalize the first letter of each word in a text file

Program:
#opening file and read a file
with open('C:/Users/rrt11/Desktop/linux.txt','r')as file:
#reading every line in file
for line in file:
print(line.title())
#title() function can be used for capitalize for everyword
Output:
Operating System Is A Software Which Operates All Hardware Compnentes.
Os Is Interface Between And A Computer Is Called Operating System
Memory Management
File Management
Security
Disk Management
Job Scheduling
Device Driver
I/O Operations
Kernal:-One Kernal And Shell
Kernal Is A Part Of The Inbuilt In Os Whatever The Command User Gives The Kernal And
Kernal Gives The Hardware
Problem 3

Script Name:: Separate_number_integer


History::
What:: Separate number from integer

Write a program to separate number from an integer to an individual digit

Program

# Take a input from user


number=input("enter a number :-")
for item in number:
print(item.split())
. #Using split method separate the integer

Output:
enter a number :- 345
['3']
['4']
['5']
Problem 4

Script Name:: Reverse_String


History::
What:: Reversing an string

Write a program to reverse an string

Program :

#Take a input from user


name=str(input("Enter your String : "))
#variable declaration
i=""
for item in name:
i=item+i
print("The reverse string is : ",i)

Output:

Enter your String : nandha


The reverse string is : ahdnan
Problem 5

Script Name:: Reverse_integer


History::
What:: Reversing an Integer

Write a program to reverse an integer

Program:
#Take a input from user
word=(input("Enter your Number :- "))
#Reversing a integer
print("The reverse of number is :-",word[::-1])

Output:
Enter your Number :- 768
The reverse of number is :- 867
Problem 6

Write a program to login to a database and export table content to a file in CSV
format

Script Name:: Database_csvfile


History::
What:: login database export table content to csv file

Program:
#Import statements
import csv
import mysql.connector
mydb = mysql.connector.connect(
#Connecting to database
host="localhost",
user="root",
passwd="rrt@11",
database="rrtintranet"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM rrtintranet.new_table")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
#Opening a file
with open('C:/Users/rrt11/Desktop/king27.csv', 'a') as csvFile:
writer = csv.writer(csvFile)
#Writing data into file
writer.writerow(x)

Output:
jagan,ck palli,programmer
kavya,Anantapur,Frontend
Nandhakishore,Malayanur,Backend
Raghu ,Anantapur,Bigdata
Problem 7

Write a program to pack an integer string into BCD encoded format

Script Name:: BCD_Format


History::
What:: integer string into BCD encoded format

Program:
#Taking a input form user
number = int(input("Enter any decimal number: "))
print("Equivalent Binary Number: ", bin(number))
#Taking a String from user
string=input("Enter a string:")
encode=' '.join(format(ord(x), 'b') for x in string)
print(encode)

Output:

Enter any decimal number: 45


Equivalent Binary Number: 0b101101
Enter a string: rrt
1110010 1110010 1110100

You might also like