Program to Take in Two Strings and Display the Larger String without Using Built-in Functions

Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions

Strings in Python:

In Python, a string may be a sequence of characters. It is an information type that has been derived. Strings are unchangeable. This means that when they have been defined, they can not be modified. Many Python functions change strings, like replace(), join(), and split(). They do not, however, alter the original string. They make a duplicate of a string, alter it, then return it to the caller.

Given a string, the task is to scan the given two strings and print the larger string without using built-in functions in python.

Examples:

Example1:

Input:

given first string =btechgeeks

given second string =python

Output:

The string btechgeeks is larger string

Example2:

Input:

given first string =java

given second string =javascript

Output:

The string javascript is larger string

Program to Take in Two Strings and Display the Larger String without Using Built-in Functions in Python

Below is the full approach is to scan the given two strings and print the larger string without using built-in functions in python.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

1)Using Count Variable and conditional statements(Static Input)

Approach:

  • Give the two strings as static input and store them in two separate variables.
  • Take a variable to say stringLength1 that stores the length of the given first string.
  • Initialize the stringLength1 to 0.
  • Take a variable to say stringLength2 that stores the length of the given second string.
  • Initialize the stringLength2 to 0.
  • Using for loop to traverse over the elements of the first string.
  • Increment the stringLength1 (Count Variable) by 1.
  • Using for loop to traverse over the elements of the second string.
  • Increment the stringLength2 (Count Variable) by 1.
  • Compare the count variables(stringLength1 ,stringLength2) of both the strings using if conditional statement.
  • If the stringlength1 is greater than stringlength2 then print the first string using the print() function.
  • If the stringlength1 is equal to stringlength2 then print both the strings are equal using the print() function.
  • If the stringlength1 is less than stringlength2 then print the second string using the print() function.
  • The Exit of the Program.

Below is the implementation:

# Give the two strings as static input and store them in two separate variables.
firststrng = 'btechgeeks'
secondstrng = 'python'
# Take a variable to say stringLength1 that stores the length of the given first string.
# Initialize the stringLength1 to 0.
stringLength1 = 0
# Take a variable to say stringLength2 that stores the length of the given second string.
# Initialize the stringLength2 to 0.
stringLength2 = 0
# Using for loop to traverse over the elements of the first string.
for charact in firststrng:
    # Increment the stringLength1 (Count Variable) by 1.
    stringLength1 = stringLength1+1
# Using for loop to traverse over the elements of the second string.
for charact in secondstrng:
    # Increment the stringLength2 (Count Variable) by 1.
    stringLength2 = stringLength2+1
# Compare the count variables(stringLength1 ,stringLength2) of both
# the strings using if conditional statement.
# If the stringlength1 is greater than stringlength2 then
# print the first string using the print() function.
if(stringLength1 > stringLength2):
    print('The string', firststrng, 'is  larger string')
# If the stringlength1 is equal to stringlength2 then
# print both the strings are equal using the print() function.
elif(stringLength1 == stringLength2):
    print('The strings', firststrng, 'and', secondstrng, 'are equal in size')
# If the stringlength1 is less than stringlength2 then
# print the second string using the print() function.
else:
    print('The string', secondstrng, 'is  larger string')

Output:

The string btechgeeks is larger string

2)Using Count Variable and conditional statements(User Input)

Approach:

  • Give the two strings as user input using the input() function and store them in two separate variables.
  • Take a variable to say stringLength1 that stores the length of the given first string.
  • Initialize the stringLength1 to 0.
  • Take a variable to say stringLength2 that stores the length of the given second string.
  • Initialize the stringLength2 to 0.
  • Using for loop to traverse over the elements of the first string.
  • Increment the stringLength1 (Count Variable) by 1.
  • Using for loop to traverse over the elements of the second string.
  • Increment the stringLength2 (Count Variable) by 1.
  • Compare the count variables(stringLength1 ,stringLength2) of both the strings using if conditional statement.
  • If the stringlength1 is greater than stringlength2 then print the first string using the print() function.
  • If the stringlength1 is equal to stringlength2 then print both the strings are equal using the print() function.
  • If the stringlength1 is less than stringlength2 then print the second string using the print() function.
  • The Exit of the Program.

Below is the implementation:

# Give the two strings as user input using the input() function
# and store them in two separate variables.
firststrng = input('Enter some random first string = ')
secondstrng = input('Enter some random second string = ')
# Take a variable to say stringLength1 that stores the length of the given first string.
# Initialize the stringLength1 to 0.
stringLength1 = 0
# Take a variable to say stringLength2 that stores the length of the given second string.
# Initialize the stringLength2 to 0.
stringLength2 = 0
# Using for loop to traverse over the elements of the first string.
for charact in firststrng:
    # Increment the stringLength1 (Count Variable) by 1.
    stringLength1 = stringLength1+1
# Using for loop to traverse over the elements of the second string.
for charact in secondstrng:
    # Increment the stringLength2 (Count Variable) by 1.
    stringLength2 = stringLength2+1
# Compare the count variables(stringLength1 ,stringLength2) of both
# the strings using if conditional statement.
# If the stringlength1 is greater than stringlength2 then
# print the first string using the print() function.
if(stringLength1 > stringLength2):
    print('The string', firststrng, 'is larger string')
# If the stringlength1 is equal to stringlength2 then
# print both the strings are equal using the print() function.
elif(stringLength1 == stringLength2):
    print('The strings', firststrng, 'and', secondstrng, 'are equal in size')
# If the stringlength1 is less than stringlength2 then
# print the second string using the print() function.
else:
    print('The string', secondstrng, 'is larger string')

Output:

Enter some random first string = vikram
Enter some random second string = vishalraj
The string vishalraj is larger string

Explanation:

  • The user must enter two strings and store them in different variables.
  • The count variables are set to zero.
  • To iterate through the characters in the strings, the for loop is utilized.
  • When a character is encountered, the count variables are incremented.
  • After that, the count variables are compared, and the longer string is printed.

Related Programs: