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 in Python by Naga
• 120 points
90 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 by Omkar
• 68,480 points

Related Questions In Python

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

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

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 3,170 points
545 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 in Python by Sagar
• 130 points
103 views
0 votes
2 answers

How to calculate square root of a number in python?

calculate square root in python >>> import math ...READ MORE

answered Apr 2 in Python by anonymous
308 views
0 votes
1 answer
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 darklord
• 6,190 points
93 views
0 votes
1 answer
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 in Python by Omkar
• 68,480 points
84 views