24msd7048(Python Lab-2).PDF (1)
24msd7048(Python Lab-2).PDF (1)
Reverse a String Problem: Write a program to reverse a string without using built-in
methods.
Algorithm:
1. Get the input string.
2. Use a loop to iterate from the end of the string to the start.
3. Append each character to a new string.
4. Print the reversed string.
Code:
def reverse_string(input_string):
reversed_string = ""
for i in range(len(input_string) - 1, -1, -1):
reversed_string += input_string[i]
return reversed_string
Output:
Code:
def is_palindrome(input_string):
# Remove spaces manually
no_spaces_string = ""
for char in input_string:
if char != " ":
no_spaces_string += char
Output:
Code:
def count_vowels_and_consonants(input_string):
vowel_count = 0
consonant_count = 0
Output:
Code:
def find_string_length(input_string):
length = 0
for char in input_string:
length += 1
return length
Code:
def convert_to_lowercase(input_string):
lowercase_string = ""
for char in input_string:
ascii_value = ord(char)
if 65 <= ascii_value <= 90: # ASCII values for uppercase letters
lowercase_string += chr(ascii_value + 32) # Convert to lowercase
else:
lowercase_string += char
return lowercase_string
Output:
Code:
def count_character_occurrence(input_string, target_char):
count = 0
for char in input_string:
if char == target_char:
count += 1
return count
# Test the function
input_string = input("Enter a string: ")
target_char = input("Enter a character to count: ")
print(f"The character '{target_char}' appears {count_character_occurrence(input_string,
target_char)} times in the string.")
Output:
Code:
def remove_spaces(input_string):
result_string = ""
for char in input_string:
if char != " ": # Check if the character is not a space
result_string += char # Add non-space characters to the result
return result_string
Output:
Output:
Code:
def replace_character(input_string, target_char, new_char):
result_string = ""
for char in input_string:
if char == target_char:
result_string += new_char # Replace the target character with the new character
else:
result_string += char # Add non-target characters to the result
return result_string
Code:
def find_longest_word(sentence):
longest_word = ""
current_word = ""
for char in sentence:
if char == " ": # Check if the character is a space
if len(current_word) > len(longest_word):
longest_word = current_word # Update the longest word
current_word = "" # Reset the current word
else:
current_word += char # Add characters to the current word
# Check the last word
if len(current_word) > len(longest_word):
longest_word = current_word
return longest_word
Output:
11. Check if Two Strings are Anagrams Problem:
Check if two strings are anagrams (i.e., they contain the same characters in a different
order).
Algorithm:
1. Count the occurrences of each character in both strings.
2. If the counts match, the strings are anagrams.
Code:
from collections import Counter
# Compare counts
return count1 == count2
# Example usage
string1 = "listen"
string2 = "silent"
print(are_anagrams(string1, string2)) # Output: True
Output:
Code:
def remove_vowels(input_string):
vowels = "aeiouAEIOU" # Include both lowercase and uppercase vowels
result = [] # Using a list for efficient string building
for char in input_string:
if char not in vowels:
result.append(char) # Append non-vowel characters
# Example usage
original_string = "Hello World"
no_vowels_string = remove_vowels(original_string)
print(no_vowels_string) # Output: Hll Wrld
Output:
Code:
def first_non_repeated_character(input_string):
seen = [] # To track characters that have been seen
repeated = [] # To track repeated characters
# Example usage
original_string = "swiss"
result = first_non_repeated_character(original_string)
print(result) # Output: "w"
Output:
Code:
def string_compression(input_string):
if not input_string: # Handle empty input
return ""
# Example usage
original_string = "aaabbbcccaa"
compressed_string = string_compression(original_string)
print(compressed_string) # Output: a3b3c3a2
Output:
Code:
def remove_character(input_string, char_to_remove):
result = "" # Initialize an empty string for the result
# Example usage
original_string = "Hello, World!"
character_to_remove = 'o'
modified_string = remove_character(original_string, character_to_remove)
print(modified_string) # Output: Hell, Wrld!
Output: