Friday 1 January 2021

Python Program to Take in Two Strings and Display 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.

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

No comments:

Post a Comment