{"id":12566,"date":"2021-07-15T19:56:38","date_gmt":"2021-07-15T14:26:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=12566"},"modified":"2021-11-22T18:38:34","modified_gmt":"2021-11-22T13:08:34","slug":"python-program-to-check-if-a-given-word-contains-consecutive-letters-using-functions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-given-word-contains-consecutive-letters-using-functions\/","title":{"rendered":"Python Program to Check if a given Word contains Consecutive Letters using Functions"},"content":{"rendered":"
Functions in Python:<\/strong><\/p>\n 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.<\/p>\n Built-in and user-defined functions are both possible. It aids the program’s ability to be concise, non-repetitive, and well-organized.<\/p>\n Given a word the task is to check if the given word contains Consecutive letters using Functions.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if the given word contains Consecutive letters using Functions.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven string = btechGeeks<\/pre>\n
The given string { btechGeeks } does not contains consecutive letters<\/pre>\n
Given string = Abtechgeeks<\/pre>\n
The given string { Abtechgeeks } contains consecutive letters<\/pre>\n
Python Program to Check if a given Word contains Consecutive Letters using Functions<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function checkConsecutive() that accepts\r\n# the given string as an argument and returns true if the given word\r\n# contains consecutive letters else returns false.\r\n\r\n\r\ndef checkConsecutive(givenstring):\r\n # Convert all of the word's characters to the upper case\r\n # because when we use ASCII values to check for\r\n # consecutive letters, we want all of the characters to be in the same case.\r\n givenstring = givenstring.upper()\r\n # Traverse the given string using For loop.\r\n # Using Python's ord() function, convert the character at the index equivalent\r\n # to the loop counter to its equivalent ASCII value.\r\n for m in range(len(givenstring)-1):\r\n # Check whether the ASCII value is one less than the ASCII value of the character\r\n # at the index comparable to the loop counter + 1.\r\n if (ord(givenstring[m]) + 1) == ord(givenstring[m+1]):\r\n # If the condition given in the earlier point is met,\r\n # the function returns true otherwise,\r\n # the function returns False\r\n return True\r\n return False\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngivenstring = \"btechGeeks\"\r\n# Pass the given string as an argument to checkConsecutive()\u00a0function.\r\nif(checkConsecutive(givenstring)):\r\n print('The given string {', givenstring, '} contains consecutive letters')\r\nelse:\r\n print('The given string {', givenstring,\r\n '} does not contains consecutive letters')\r\n<\/pre>\n
The given string { btechGeeks } does not contains consecutive letters<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function checkConsecutive() that accepts\r\n# the given string as an argument and returns true if the given word\r\n# contains consecutive letters else returns false.\r\n\r\n\r\ndef checkConsecutive(givenstring):\r\n # Convert all of the word's characters to the upper case\r\n # because when we use ASCII values to check for\r\n # consecutive letters, we want all of the characters to be in the same case.\r\n givenstring = givenstring.upper()\r\n # Traverse the given string using For loop.\r\n # Using Python's ord() function, convert the character at the index equivalent\r\n # to the loop counter to its equivalent ASCII value.\r\n for m in range(len(givenstring)-1):\r\n # Check whether the ASCII value is one less than the ASCII value of the character\r\n # at the index comparable to the loop counter + 1.\r\n if (ord(givenstring[m]) + 1) == ord(givenstring[m+1]):\r\n # If the condition given in the earlier point is met,\r\n # the function returns true otherwise,\r\n # the function returns False\r\n return True\r\n return False\r\n\r\n\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngivenstring = input('Enter some random string = ')\r\n# Pass the given string as an argument to checkConsecutive()\u00a0function.\r\nif(checkConsecutive(givenstring)):\r\n print('The given string {', givenstring, '} contains consecutive letters')\r\nelse:\r\n print('The given string {', givenstring,\r\n '} does not contains consecutive letters')\r\n<\/pre>\n
Enter some random string = Abtechgeeks\r\nThe given string { Abtechgeeks } contains consecutive letters<\/pre>\n
\n