String Manipulation Programs (Class 11th)
1. Reversing a String
# Program to reverse a string
string = input("Enter a string: ")
reversed_string = string[::-1]
print("Reversed string:", reversed_string)
2. Check if a String is a Palindrome
# Program to check for palindrome
string = input("Enter a string: ").lower()
if string == string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
3. Count Vowels and Consonants
# Program to count vowels and consonants
string = input("Enter a string: ").lower()
vowels = "aeiou"
vowel_count = consonant_count = 0
for char in string:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
print("Vowels:", vowel_count)
print("Consonants:", consonant_count)
4. Convert Uppercase to Lowercase and Vice Versa
# Program to toggle case
string = input("Enter a string: ")
toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in
string])
print("Toggled string:", toggled_string)
5. Count Occurrences of a Character
# Program to count occurrences of a character
string = input("Enter a string: ")
char = input("Enter a character to find its frequency: ")
frequency = string.count(char)
print(f"The character '{char}' appears {frequency} times.")
6. Find Longest Word in a Sentence
# Program to find the longest word
sentence = input("Enter a sentence: ")
words = sentence.split()
longest_word = max(words, key=len)
print("Longest word:", longest_word)
print("Length of the longest word:", len(longest_word))
7. Reverse Each Word in a Sentence
# Program to reverse each word
sentence = input("Enter a sentence: ")
reversed_words = ' '.join([word[::-1] for word in sentence.split()])
print("Reversed words:", reversed_words)
8. Extract and Sum Digits in a String
# Program to sum digits in a string
string = input("Enter a string: ")
digit_sum = sum(int(char) for char in string if char.isdigit())
print("Sum of digits in the string:", digit_sum)
9. Check if Two Strings are Anagrams
# Program to check for anagrams
string1 = input("Enter the first string: ").lower()
string2 = input("Enter the second string: ").lower()
if sorted(string1) == sorted(string2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")
10. Remove Duplicate Characters
# Program to remove duplicate characters
string = input("Enter a string: ")
unique_chars = ''.join(sorted(set(string), key=string.index))
print("String after removing duplicates:", unique_chars)
11. Count Words Starting with a Specific Letter
# Program to count words starting with a specific letter
sentence = input("Enter a sentence: ").lower()
letter = input("Enter the starting letter: ").lower()
count = sum(1 for word in sentence.split() if word.startswith(letter))
print(f"Number of words starting with '{letter}':", count)
12. Sort Characters Alphabetically
# Program to sort characters alphabetically
string = input("Enter a string: ")
sorted_string = ''.join(sorted(string))
print("Alphabetically sorted string:", sorted_string)