Python Program to Find the Larger String without Using Built-in Functions

String comparison is a fundamental operation in programming. This program showcases how to create a Python program to find the larger of two input strings without relying on built-in functions.

Problem Statement

Write a Python program that takes two strings as input and determines which one is larger in terms of character length, without using any built-in length-related functions.

Python Program to Find the Larger String without Using Built-in Functions

def find_larger_string(str1, str2):
    len1 = 0
    len2 = 0

    for char in str1:
        len1 += 1

    for char in str2:
        len2 += 1

    if len1 > len2:
        return str1
    elif len2 > len1:
        return str2
    else:
        return "Both strings have the same length."

# Input
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")

# Finding the larger string
larger_string = find_larger_string(string1, string2)

# Output
print("The larger string is:", larger_string)

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...