Tuesday, July 26, 2022

How to count string characters using loops?

A string of characters can be counted using for or while loop. In the following example, we will demonstrate how to create a program that will count each letter.
Example 1 Create a program that will count all characters in the string defined by the user and show the counted values as the output.
Solution: The first step is to create all lowercase letters followed by a dot, semicolon, and comma
lowerCase = "abcdefghijklmnopqrstuvwxyz.;,"
Then we will create 30 variables that will be counters for each letter.
lowerCase = "abcdefghijklmnopqrstuvwxyz.;,"
#Creating counter variables
COUNT_A = 0; COUNT_B = 0; COUNT_C = 0;
COUNT_D = 0; COUNT_E = 0; COUNT_F = 0;
COUNT_G = 0; COUNT_H = 0; COUNT_I = 0;
COUNT_J = 0; COUNT_K = 0; COUNT_L = 0;
COUNT_M = 0; COUNT_N = 0; COUNT_O = 0;
COUNT_P = 0; COUNT_Q = 0; COUNT_R = 0;
COUNT_S = 0; COUNT_T = 0; COUNT_U = 0;
COUNT_V = 0; COUNT_W = 0; COUNT_X = 0;
COUNT_Y = 0; COUNT_Z = 0; COUNT_DOT = 0;
COUNT_SEMICOL = 0; COUNT_COMMA = 0;
#################################################
# User input string
#################################################
USER_INPUT = input("Enter a string: ").lower()
for letter in USER_INPUT:
if letter == lowerCase[0]:
COUNT_A += 1
elif letter == lowerCase[1]:
COUNT_B += 1
elif letter == lowerCase[2]:
COUNT_C += 1
elif letter == lowerCase[3]:
COUNT_D += 1
elif letter == lowerCase[4]:
COUNT_E += 1
elif letter == lowerCase[5]:
COUNT_F += 1
elif letter == lowerCase[6]:
COUNT_G += 1
elif letter == lowerCase[7]:
COUNT_H += 1
elif letter == lowerCase[8]:
COUNT_I += 1
elif letter == lowerCase[9]:
COUNT_J += 1
elif letter == lowerCase[10]:
COUNT_K += 1
elif letter == lowerCase[11]:
COUNT_L += 1
elif letter == lowerCase[12]:
COUNT_M += 1
elif letter == lowerCase[13]:
COUNT_N += 1
elif letter == lowerCase[14]:
COUNT_O += 1
elif letter == lowerCase[15]:
COUNT_P += 1
elif letter == lowerCase[16]:
COUNT_Q += 1
elif letter == lowerCase[17]:
COUNT_R += 1
elif letter == lowerCase[18]:
COUNT_S += 1
elif letter == lowerCase[19]:
COUNT_T += 1
elif letter == lowerCase[20]:
COUNT_U += 1
elif letter == lowerCase[21]:
COUNT_V += 1
elif letter == lowerCase[22]:
COUNT_W += 1
elif letter == lowerCase[23]:
COUNT_X += 1
elif letter == lowerCase[24]:
COUNT_Y += 1
elif letter == lowerCase[25]:
COUNT_Z += 1
elif letter == lowerCase[26]:
COUNT_DOT += 1
elif letter == lowerCase[27]:
COUNT_SEMICOL += 1
elif letter == lowerCase[28]:
COUNT_COMMA += 1
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[0], COUNT_A))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[1], COUNT_B))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[2], COUNT_C))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[3], COUNT_D))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[4], COUNT_E))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[5], COUNT_F))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[6], COUNT_G))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[7], COUNT_H))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[8], COUNT_I))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[9], COUNT_J))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[10], COUNT_K))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[11], COUNT_L))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[12], COUNT_M))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[13], COUNT_N))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[14], COUNT_O))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[15], COUNT_P))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[16], COUNT_Q))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[17], COUNT_R))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[18], COUNT_S))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[19], COUNT_T))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[20], COUNT_U))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[21], COUNT_V))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[22], COUNT_W))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[23], COUNT_X))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[24], COUNT_Y))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[25], COUNT_Z))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[26], COUNT_DOT))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[27], COUNT_SEMICOL))
print("The number of letter '{}' in USER_INPUT are {}".format(lowerCase[28], COUNT_COMMA))
The previous program seems complicated but it is very simple. The program starts by declaring all lowercase characters of the English alphabet including the dot, semicolon, and comma. All these characters together create a string that is assigned to a variable name lowerCase. After this variable, the 29 variables were created and integer 0 is assigned to each variable. The values of these variables will be changed later in for loop each time the Python encounters a specific character of a string entered by the user. In the next command line, the input function is used that asks a user to enter a string. If user enters a string with capital letters these capital letters will be lowered using lower() function and the string will be assigned to the variable named USER_INPUT.
In the for loop each character of a string entered by the user is checked if it is contained in lowerCase variable. If the character of a string defined by user is located in the loweCase variable than the value of the count variable for specific character will be updated by 1. After each character of a string is checked the values of all count variables is shown as output using print function. In the following output, the user has typed the string "This program is very cool." after which the program showed the values of all count variables.
Output:
Enter a string: This program is very cool.
The number of letter 'a' in USER_INPUT are 1
The number of letter 'b' in USER_INPUT are 0
The number of letter 'c' in USER_INPUT are 1
The number of letter 'd' in USER_INPUT are 0
The number of letter 'e' in USER_INPUT are 1
The number of letter 'f' in USER_INPUT are 0
The number of letter 'g' in USER_INPUT are 1
The number of letter 'h' in USER_INPUT are 1
The number of letter 'i' in USER_INPUT are 2
The number of letter 'j' in USER_INPUT are 0
The number of letter 'k' in USER_INPUT are 0
The number of letter 'l' in USER_INPUT are 1
The number of letter 'm' in USER_INPUT are 1
The number of letter 'n' in USER_INPUT are 0
The number of letter 'o' in USER_INPUT are 3
The number of letter 'p' in USER_INPUT are 1
The number of letter 'q' in USER_INPUT are 0
The number of letter 'r' in USER_INPUT are 3
The number of letter 's' in USER_INPUT are 2
The number of letter 't' in USER_INPUT are 1
The number of letter 'u' in USER_INPUT are 0
The number of letter 'v' in USER_INPUT are 1
The number of letter 'w' in USER_INPUT are 0
The number of letter 'x' in USER_INPUT are 0
The number of letter 'y' in USER_INPUT are 1
The number of letter 'z' in USER_INPUT are 0
The number of letter '.' in USER_INPUT are 1
The number of letter ';' in USER_INPUT are 0
The number of letter ',' in USER_INPUT are 0

No comments:

Post a Comment