0% found this document useful (0 votes)
28 views5 pages

Ap 5

Uploaded by

Vaibhav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Ap 5

Uploaded by

Vaibhav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 5

Student Name: Vaibhav Sharma UID: 22BCS12459


Branch: BE – CSE Section/Group: 22BCS_ML-901 - A
th
Semester: 5 Date of Performance: 13/08/24
Subject Name: Advanced Programming Lab-1
Subject Code: 22CSP-314

Problem 1 (Find a String)


1. Aim:
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.

NOTE: String letters are case-sensitive.

Input Format

The first line of input contains the original string. The next line contains the substring.

Constraints

Each character in the string is an ascii character.

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()

count = count_substring(string, sub_string)


print(count)

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.

• Iterate Through the String:

• Loop through each character c in the input string.


• Check if the character is an alphabetic character using isalpha(c).
• If the character is alphabetic, convert it to lowercase using tolower(c).
• Update the count array by incrementing the index corresponding to that letter. The
index is determined by subtracting 'a' from the character, i.e., count[tolower(c) -
'a'].

• Check the Count Array:

• 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:

• If the string is a pangram, return "pangram".


• Otherwise, return "not pangram".

3. Source Code:
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool isPangram(const string& str) {


int count[26] = {0};

for (char c : str) {


if (isalpha(c)) {
count[tolower(c) - 'a']++;
}
}

for (int i = 0; i < 26; ++i) {


if (count[i] == 0) {
return false;
}
}

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.

You might also like