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
389 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,030 points

Related Questions In Python

+1 vote
1 answer
0 votes
0 answers
–1 vote
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, 2019 in Python by Mohammad
• 3,210 points
8,260 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
381 views
0 votes
1 answer

Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list.

Hi,  num_words_list = len(original_str.split()) original_str.split() - split words in ...READ MORE

answered May 27 in Python by Niroj
• 58,500 points
908 views
0 votes
1 answer

How to split a large sample of text into words in Python?

Hi@akhtar, You can use nltk module to split your text ...READ MORE

answered Jun 26 in Python by MD
• 56,480 points
59 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,320 points
149 views
0 votes
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,030 points
195 views