Python Program to Check if a given Word contains Consecutive Letters using Functions

Functions in Python:

In Python, a function is a grouping of connected statements that performs a computational, logical, or evaluative activity. The idea is to combine some often or repeatedly performed tasks into a function, so that instead of writing the same code for different inputs again and over, we may call the function and reuse the code contained within it.

Built-in and user-defined functions are both possible. It aids the program’s ability to be concise, non-repetitive, and well-organized.

Given a word the task is to check if the given word contains Consecutive letters using Functions.

Examples:

Example1:

Input:

Given string = btechGeeks

Output:

The given string { btechGeeks } does not contains consecutive letters

Example2:

Input:

Given string = Abtechgeeks

Output:

The given string { Abtechgeeks } contains consecutive letters

Python Program to Check if a given Word contains Consecutive Letters using Functions

Below are the ways to check if the given word contains Consecutive letters using Functions.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Create a function checkConsecutive() that accepts the given string as an argument and returns true if the given word contains consecutive letters else returns false.
  • Pass the given string as an argument to checkConsecutive() function.
  • Convert all of the word’s characters to the upper case because when we use ASCII values to check for consecutive letters, we want all of the characters to be in the same case.
  • Traverse the given string using For loop.
  • Using Python’s ord() function, convert the character at the index equivalent to the loop counter to its equivalent ASCII value.
  • Check whether the ASCII value is one less than the ASCII value of the character at the index comparable to the loop counter + 1.
  • If the condition given in the earlier point is met, the function returns true otherwise, the function returns False.
  • Print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkConsecutive() that accepts
# the given string as an argument and returns true if the given word
# contains consecutive letters else returns false.


def checkConsecutive(givenstring):
  # Convert all of the word's characters to the upper case
  # because when we use ASCII values to check for
  # consecutive letters, we want all of the characters to be in the same case.
    givenstring = givenstring.upper()
    # Traverse the given string using For loop.
    # Using Python's ord() function, convert the character at the index equivalent
    # to the loop counter to its equivalent ASCII value.
    for m in range(len(givenstring)-1):
      # Check whether the ASCII value is one less than the ASCII value of the character
      # at the index comparable to the loop counter + 1.
        if (ord(givenstring[m]) + 1) == ord(givenstring[m+1]):
          # If the condition given in the earlier point is met,
          # the function returns true otherwise,
          # the function returns False
            return True
    return False


# Give the string as static input and store it in a variable.
givenstring = "btechGeeks"
# Pass the given string as an argument to checkConsecutive() function.
if(checkConsecutive(givenstring)):
    print('The given string {', givenstring, '} contains consecutive letters')
else:
    print('The given string {', givenstring,
          '} does not contains consecutive letters')

Output:

The given string { btechGeeks } does not contains consecutive letters

Method #2: Using For Loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Create a function checkConsecutive() that accepts the given string as an argument and returns true if the given word contains consecutive letters else returns false.
  • Pass the given string as an argument to checkConsecutive() function.
  • Convert all of the word’s characters to the upper case because when we use ASCII values to check for consecutive letters, we want all of the characters to be in the same case.
  • Traverse the given string using For loop.
  • Using Python’s ord() function, convert the character at the index equivalent to the loop counter to its equivalent ASCII value.
  • Check whether the ASCII value is one less than the ASCII value of the character at the index comparable to the loop counter + 1 using ord() function.
  • If the condition given in the earlier point is met, the function returns true otherwise, the function returns False.
  • Print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a function checkConsecutive() that accepts
# the given string as an argument and returns true if the given word
# contains consecutive letters else returns false.


def checkConsecutive(givenstring):
  # Convert all of the word's characters to the upper case
  # because when we use ASCII values to check for
  # consecutive letters, we want all of the characters to be in the same case.
    givenstring = givenstring.upper()
    # Traverse the given string using For loop.
    # Using Python's ord() function, convert the character at the index equivalent
    # to the loop counter to its equivalent ASCII value.
    for m in range(len(givenstring)-1):
      # Check whether the ASCII value is one less than the ASCII value of the character
      # at the index comparable to the loop counter + 1.
        if (ord(givenstring[m]) + 1) == ord(givenstring[m+1]):
          # If the condition given in the earlier point is met,
          # the function returns true otherwise,
          # the function returns False
            return True
    return False


# Give the string as user input using the input() function and store it in a variable.
givenstring = input('Enter some random string = ')
# Pass the given string as an argument to checkConsecutive() function.
if(checkConsecutive(givenstring)):
    print('The given string {', givenstring, '} contains consecutive letters')
else:
    print('The given string {', givenstring,
          '} does not contains consecutive letters')

Output:

Enter some random string = Abtechgeeks
The given string { Abtechgeeks } contains consecutive letters

Related Programs: