How to write a program that counts number of characters number of words number of repeated words and number of repeated characters in a text file using opps concept in python

0 votes
Oct 26, 2019 in Python by Naga
• 120 points
2,021 views

1 answer to this question.

0 votes

Here's the logic. You have to add the code to take input from text file: 

ch_count = 0
word_count = 1
chars = []
words = []
chars1 = []
words1 = []
rep_char = 0
rep_word = 0
word_i = 0

if test:
    for i in range(0,len(test)):
        if test[i] is not " ":
            ch_count+=1
            if test[i] in chars:
                if test[i] not in chars1:
                    chars1.append(test[i])
                    rep_char+=1
            else:
                chars.append(test[i])

        else:
            word_count+=1
            print(test[word_i:i])
            if test[word_i:i] in words:
                if test[word_i:i] not in words1:
                    words1.append(test[word_i:i])
                    rep_word+=1
            else:
                words.append(test[word_i:i])
            print(words, words1)

            try:
                word_i = i+1
            except:
                pass
    print(test[word_i:])
    if test[word_i:] in words:
        if test[word_i:] not in words1:
            rep_word+=1



if test[-1] is " ":
    word_count-=1


print('Total number of characters: '+str(ch_count))
print('Total number of words: '+str(word_count))
print('Repeated characters: '+str(rep_char))
print('Repeated Words: '+str(rep_word))
answered Oct 28, 2019 by Omkar
• 69,220 points

Related Questions In Python

+1 vote
1 answer
0 votes
0 answers
0 votes
1 answer

How to find the value of a row in a csv file in python and print column and row of that value ?

Hello @Khanhh , Use panda to find the value of ...READ MORE

answered Oct 15, 2020 in Python by Niroj
• 82,840 points
6,815 views
–1 vote
2 answers
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,886 views
+1 vote
0 answers

Sum the values of column matching and not matching from a .txt file and write output to a two different files using Python

Name                                                    value DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 15657 DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 156579 DR_CNDAOFSZAPZP_GPFS_VOL.2 156579 DR_CNDAOFSZAPZP_GPFS_VOL.3 ...READ MORE

Nov 20, 2019 in Python by Sagar
• 130 points
1,219 views
0 votes
1 answer

What is the syntax of USE statement in MySQL?

The USE statement is used to mention ...READ MORE

answered Nov 27, 2018 in Database by Sahiti
• 6,370 points
795 views
–1 vote
2 answers
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
1,617 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP