Ap 5
Ap 5
times that the substring occurs in the given string. String traversal will take place from left to
Input Format
The first line of input contains the original string. The next line contains the substring.
Constraints
2. Objective:
In this challenge, the user enters a string and a substring. You have to print the number of
times that the substring occurs in the given string. String traversal will take place from left
to right, not from right to left.
3. Algorithm:
1. Initialization:
o Start with a count set to 0. This will keep track of the number of occurrences
of the substring.
o Calculate the length of the substring, as it will be used to slice the original
string in each iteration.
2. Loop through the String:
o Iterate through the string, stopping the loop when there aren't enough
characters left in the string to form the substring.
oIn each iteration, slice the string from the current index i to i + length
(where length is the length of the substring).
o Check if this slice is equal to the substring.
3. Increment Count:
o If the sliced string matches the substring, increment the count by 1.
4. Return the Count:
o Once the loop completes, return the count, which is the number of times the
substring appears in the original string.
4. Source Code:
def count_substring(string, sub_string):
count = 0
length = len(sub_string)
for i in range(len(string) - length + 1):
if string[i:i+length] == sub_string:
count += 1
return count
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
5. Output:
6. Time Complexity: O(n*m)
Problem 2 ()
a. Aim:
A pangram is a string that contains every letter of the alphabet. Given a sentence determine
whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not
pangram as appropriate.
Example
The string contains all letters in the English alphabet, so return pangram.
Function Description
Complete the function pangrams in the editor below. It should return the string pangram if
the input string is a pangram. Otherwise, it should return not pangram.
pangrams has the following parameter(s):
• string s: a string to test
Returns
• string: either pangram or not pangram
b. Objective:
A pangram is a string that contains every letter of the alphabet. Given a sentence determine
whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not
pangram as appropriate.
3. Algorithm:
• Initialization:
• We create an array count[26] to keep track of the occurrence of each letter of the
alphabet. The array is initialized to zero.
• After processing all characters in the string, iterate through the count array.
• If any element in the count array is zero, it means that the corresponding letter of the
alphabet did not appear in the string, so the string is not a pangram.
• If all elements are greater than zero, the string is a pangram.
• Return Result:
3. Source Code:
#include <iostream>
#include <string>
#include <cctype>
return true;
}
int main() {
string s;
getline(cin, s);
if (isPangram(s)) {
cout << "pangram" << endl;
} else {
cout << "not pangram" << endl;
}
return 0;
}
4. Output:
5. Time Complexity: O(n)
6. Learning Outcomes:
• Learned about string traversal techniques and slicing operations.
• Learned how to compare and match substrings within a larger string.
• Learned about pangrams and their significance in string processing.
• Learned how to use arrays to track the presence of characters.
• Learned to implement character checks using ASCII manipulation.