Please don't spam
Write a program to count the number of times a string is repeated in another string.
Answers
Answered by
7
str1 = input("Enter a string: ")
str2 = input("Enter a second string: ")
cstr = 0
for i in str1.split():
if i == str2:
cstr = cstr + 1
print(str2, "is repeated in the first string", cstr, "times.")
split() is a string function that splits every element in the string, separated by a space character.
For example:
>>> str1 = "Hello World"
>>> str1.split()
['Hello', 'World']
You can even use the function to split characters after a specified character.
>>> str1 = "This#is#so#fun!"
>>> str1.split("#")
['This', 'is', 'so', 'fun!']
Attachments:
Equestriadash:
Thanks for the Brainliest! ^_^"
Answered by
71
Answer:
The program output is also shown below.
* C Program To Count the Occurrence of a Substring in String :
- #include <string.h>
- char str[100], sub[100];
- int count = 0, count1 = 0;
- int i, j, l, l1, l2;
- printf("\nEnter a string : ");
- scanf("%[^\n]s", str);
- l1 = strlen(str);
Similar questions