Practical 11: program that accepts a string from the user and performs the following string operations.
Problem Statement
Write a Python program that accepts a string from the user and performs the following string operations:
- Calculate the length of the string.
- Reverse the string.
- Check the equality of two strings.
- Check whether the string is a palindrome.
- Check if one string is a substring of another.
Solution Code
# Function to calculate the length of a string
def string_length(s):
count = 0
for _ in s:
count += 1
return count
# Function to reverse a string
def reverse_string(s):
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
return reversed_s
# Function to check if two strings are equal
def check_equality(s1, s2):
return s1 == s2
# Function to check if a string is a palindrome
def is_palindrome(s):
return s == reverse_string(s)
# Function to check if a string is a substring of another
def is_substring(main_str, sub_str):
return sub_str in main_str
# Taking input from the user
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
# Performing operations
print("Length of first string:", string_length(string1))
print("Reversed first string:", reverse_string(string1))
print("Are both strings equal?", check_equality(string1, string2))
print("Is the first string a palindrome?", is_palindrome(string1))
print(f"Is '{string2}' a substring of '{string1}'?", is_substring(string1, string2))
Output
- The program takes two strings as input.
- It calculates the length of the first string.
- It reverses the first string.
- It checks whether both strings are equal.
- It checks whether the first string is a palindrome.
- It verifies if the second string is a substring of the first string.
Example Output
Enter the first string: racecar
Enter the second string: car
Length of first string: 7
Reversed first string: racecar
Are both strings equal? False
Is the first string a palindrome? True Is 'car' a substring of 'racecar'? True
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 3 Practical 11 solution where we have written Python program that accepts a string from the user and performs the following string operations.
- Calculating String Length:
- The function string_length() iterates through the string and counts the characters manually.
- Reversing the String:
- The function reverse_string() constructs a reversed string by adding each character at the beginning.
- Checking Equality of Two Strings:
- The function check_equality() compares both strings using ==.
- Checking for Palindrome:
- The function is_palindrome() compares the original string with its reversed version.
- Checking for Substring:
- The function is_substring() uses the in operator to check if the second string is a part of the first.
Key Concepts Learned
- String Length Calculation Without len()
- Manual String Reversal Without [::-1]
- String Comparisons Using ==
- Checking Palindromes Using String Reversal
- Substring Checking Using in Operator