string - Python: Most frequent character counter -


i trying create program asks user input string , displays occurring characters in string. cannot use built in features such dictionary. think problem have infinite loop. how can fix this? here code:

string = input('enter string:')  #space popular character pop_char = '' #initialize number of times character appears num = 0 #initialize index index = 0  # pick 1 character @ time ch in string:     #initialize character counter     cc=0  #go through entire string , compare character string characters     while index < len(string):          #if same (converted lower case), tally 1 cc         if ch.lower() == string[index].lower():             cc = cc + 1             index = index + 1          #if count greater or equal previous count, use         #popular character         if cc >= num:             num = cc             pop_char = ch          else:             index = index + 1 print('the occuring letter(s):', pop_char) 

i found problem:

if cc >= num: test should be: if cc > num:

on first iteration first letter equal first string letter (obviously) , enter if ch.lower() == string[index].lower():. set cc 1 will, in turn, set num 1 well. both equal, , long second letter not same first, have infinite loop enters if cc >= num section , never update index past 1. removed = , runs fine =)


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -