Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given two strings. The task is to find the larger string without using built-in functions.

Examples:

Input:
GeeksforGeeks
Geeks
Output:
GeeksforGeeks

Input:
GeeksForFeeks is an good Computer Coding Website
It offers several topics
Output:
GeeksForFeeks is an good Computer Coding Website

Step-by-step Approach:

  • Take two strings in separate variables.
  • Initialize the two count variables to zero.
  • Use a for loop to traverse through the characters in the string and increment the count variables each time a character is encountered.
  • Compare the count variables of both the strings.
  • Print the larger string.
  • Exit.

Below is the complete program based on the above approach:

Python3

string1="GeeksForFeeks is an good Computer Coding Website "
string2="It offers several topics"
count1=0
count2=0
 
for i in string1:
      count1=count1+1
for j in string2:
      count2=count2+1
 
if(count1<count2):
      print("Larger string is:")
      print(string2)
 
elif(count1==count2):
      print("Both strings are equal.")
 
else:
      print("Larger string is:")
      print(string1)

                    

Output
Larger string is:
GeeksForFeeks is an good Computer Coding Website 

Time Complexity: O(n)
Auxiliary Space: O(1)

Program Explanation :

  1. User must enter two strings and store it in separate variables.
  2. The count variables are initialized to zero.
  3. The for loop is used to traverse through the characters in the strings.
  4. The count variables are incremented each time a character is encountered.
  5. The count variables are then compared and the larger string is printed.

Using try/except:

Here’s a solution that uses try/except and string indexing: In this solution, we use a while loop to iterate over the characters of both strings, incrementing the indices i and j each time. We use a try-except block to handle the IndexError that is raised when we try to access an index that is outside the bounds of the string. If an IndexError is raised, it means that one of the strings has ended, so we compare the final values of i and j to determine which string is longer. If i is greater than j, it means str1 is longer and we return it. Otherwise, we return str2.

Python3

def larger_string(str1, str2):
    i = 0
    j = 0
    try:
        while True:
            i += 1
            j += 1
            str1[i]
            str2[j]
    except IndexError:
        if i > j:
            return str2
        else:
            return str1
 
string1 = "GeeksForFeeks is an good Computer Coding Website "
string2 = "It offers several topics"
print("Larger string is:")
print(larger_string(string1, string2))

                    

Output
Larger string is:
GeeksForFeeks is an good Computer Coding Website 

This solution has a time complexity of O(n), where n is the length of the longer string, because the while loop will run n times at most. The space complexity is O(1), because we only use a few variables to store the indices and the result, and the size of these variables does not depend on the length of the strings.



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads