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

«
»

This is a Python Program to take in two strings and display the larger string without using built-in functions.

Problem Description

The program takes in two strings and display the larger string without using built-in function.

Problem Solution

1. Take in two strings from the user and store it in separate variables.
2. Initialize the two count variables to zero.
3. Use a for loop to traverse through the characters in the string and increment the count variables each time a character is encountered.
4. Compare the count variables of both the strings.
5. Print the larger string.
6. Exit.

Program/Source Code

Here is source code of the Python Program to take in two strings and display the larger string without using built-in functions. The program output is also shown below.

string1=raw_input("Enter first string:")
string2=raw_input("Enter second string:")
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)
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.
6. The count variables are then compared and the larger string is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter first string:Bangalore
Enter second string:Delhi
Larger string is:
Bangalore
 
Case 2:
Enter first string:Ball
Enter second string:Apple
Larger string is:
Apple

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.