In: Computer Science
# PYTHON
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
Ex: If the input is:
n Monday
the output is:
1
Ex: If the input is:
z Today is Monday
the output is:
0
Ex: If the input is:
n It's a sunny day
the output is:
2
Case matters.
Ex: If the input is:
n Nobody
the output is:
0
n is different than N.
To implement the given program, we will first take a user input string 'inputString' and store its first character to a variable 'a'. Next, we create a counter 'count' and initialize it to 0.Then, we iterate through the string from index 1 to length of the string.If a character is encountered which matches the character assigned to 'a' then we increase the counter. In the end, we print the count.
Code :
#taking input character and string inputString = input() #storing the first character to check a = inputString[0] #setting counter count = 0 #traverse the string from index 1 to end for element in range(1, len(inputString)): if inputString[element]==a : #update counter count = count + 1 #print count print(count)
Output 1:
Output 2:
Output 3:
Output 4:
Note : Please keep track of the indentation while coding in python.