Thursday, 9 January 2025

Day 82 : Python Program to Find the Larger String without using Built in Functions

 


def get_length(string):

    length = 0

    for char in string:

        length += 1

    return length

def find_larger_string(string1, string2):

    length1 = get_length(string1)

    length2 = get_length(string2)

        if length1 > length2:

        return string1

    elif length2 > length1:

        return string2

    else:

        return "Both strings are of equal length."

string1 = input("Enter the first string: ")

string2 = input("Enter the second string: ")

larger_string = find_larger_string(string1, string2)

print("Larger string:", larger_string)

#source code --> clcoding.com 


Code Explanation:

First Function: get_length
def get_length(string):
This line defines a function called get_length, which takes one argument, string. This argument is expected to be a string, and the function will return its length.

    length = 0
Inside the function, a variable length is initialized to 0. This will be used to count the number of characters in the string.

    for char in string:
This line starts a for loop that iterates over each character (char) in the given string.

        length += 1
Each time the loop processes a character in the string, it increments the length by 1. This keeps track of how many characters have been encountered.

    return length
Once the loop finishes (i.e., all characters in the string have been counted), the function returns the final value of length, which is the total number of characters in the string.
Second Function: find_larger_string

def find_larger_string(string1, string2):
This defines the find_larger_string function, which takes two strings as arguments: string1 and string2. The goal is to compare the lengths of these two strings and determine which one is longer.

    length1 = get_length(string1)
The get_length function is called with string1 as the argument. The returned length is stored in the variable length1.

    length2 = get_length(string2)
Similarly, the get_length function is called with string2 as the argument, and the returned length is stored in the variable length2.

    if length1 > length2:
This line checks if the length of string1 (length1) is greater than the length of string2 (length2).
If this condition is true, it means string1 is longer than string2.

        return string1
If length1 > length2, this line returns string1 because it is the longer string.

    elif length2 > length1:
This line checks if the length of string2 is greater than the length of string1. If this condition is true, it means string2 is longer.

        return string2
If length2 > length1, this line returns string2 because it is the longer string.

    else:
This line is the else part of the condition. If neither length1 > length2 nor length2 > length1 is true, it means the two strings are of equal length.

        return "Both strings are of equal length."
If the lengths of both strings are equal, the function returns the message "Both strings are of equal length.".

Taking Input from the User
string1 = input("Enter the first string: ")
This line prompts the user to input the first string and stores the input in the variable string1.

string2 = input("Enter the second string: ")
This line prompts the user to input the second string and stores the input in the variable string2.

Calling the Function and Printing the Result
larger_string = find_larger_string(string1, string2)
This line calls the find_larger_string function with string1 and string2 as arguments and stores the returned result (either the longer string or a message) in the variable larger_string.

print("Larger string:", larger_string)
This line prints the result stored in larger_string, which is either the longer string or the message "Both strings are of equal length.".

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (80) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (953) Python Coding Challenge (398) Python Quiz (53) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses