Friday, September 5, 2025
HomeLanguagesPython Program to Count characters surrounding vowels

Python Program to Count characters surrounding vowels

Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors.

Examples:

Input : test_str = ‘neveropenforneveropen’ 
Output : 10 
Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels.

Input : test_str = ‘neveropen’ 
Output : 2 
Explanation : g, k have surrounding vowels. 
 

Method 1 : Using loop

In this, we increment counter while checking previous and successive element for vowels using loop.

Python3




# initializing string
test_str = 'neveropenforneveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
res = 0
vow_list = ['a', 'e', 'i', 'o', 'u']
for idx in range(1, len(test_str) - 1):
 
    # checking for preceding and succeeding element to be vowel
    if test_str[idx] not in vow_list and (test_str[idx - 1] in vow_list or test_str[idx + 1] in vow_list):
        res += 1
 
# solving for 1st and last element
if test_str[0] not in vow_list and test_str[1] in vow_list:
    res += 1
 
if test_str[-1] not in vow_list and test_str[-2] in vow_list:
    res += 1
 
# printing result
print("Characters around vowels count : " + str(res))


Output:

The original string is :neveropenforneveropen

Characters around vowels count : 10

Method 2 : Using sum() and list comprehension

In this, we perform the task of getting count using sum() and iteration and filtering is done using list comprehension.

Python3




# initializing string
test_str = 'neveropenforneveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
vow_list = ['a', 'e', 'i', 'o', 'u']
 
# sum() accumulates all vowels surround elements
res = sum([1 for idx in range(1, len(test_str) - 1) if test_str[idx]
           not in vow_list and (test_str[idx - 1] in vow_list or test_str[idx + 1] in vow_list)])
 
# solving for 1st and last element
if test_str[0] not in vow_list and test_str[1] in vow_list:
    res += 1
 
if test_str[-1] not in vow_list and test_str[-2] in vow_list:
    res += 1
 
# printing result
print("Characters around vowels count : " + str(res))


 Output:

The original string is :neveropenforneveropen

Characters around vowels count : 10

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Space Complexity: O(n)

Method 3: Using enumerate () method.

Python3




# initializing string
test_str = 'neveropenforneveropen'
 
# printing original string
print("The original string is : " + str(test_str))
 
count=0
 
#counting vowel surrounding characters using enumerate() method
 
for idx, val in enumerate(test_str):
  if val in 'aeiouAEIOU':
    if idx > 0 and test_str[idx-1] not in 'aeiouAEIOU':
      count += 1
    if idx < len(test_str) - 1 and test_str[idx+1] not in 'aeiouAEIOU':
       count += 1
 
# printing result
print("Characters around vowels count : " + str(count))


Output

The original string is :neveropenforneveropen
Characters around vowels count : 10

Time Complexity:  O(n)

Auxiliary Space: O(1)

Method 4: Using Regex

Explanation
In this approach, we use the re.findall() method to find the characters that are surrounded by vowels.
The regular expression r'[^aeiouAEIOU]([aeiouAEIOU])[^aeiouAEIOU]’ matches a character that is not a vowel, followed by a vowel, followed by a character that is not a vowel.
The len() function is used to count the number of matches found.
 

Python3




#Importing the regular expression module
import re
 
#initializing string
test_str = 'neveropenforneveropen'
 
#printing original string
print("The original string is : " + str(test_str))
 
#Using re.findall() to find the number of characters surrounded by vowels
res = len(re.findall(r'[^aeiouAEIOU]([aeiouAEIOU])', test_str)) + len(re.findall(r'([aeiouAEIOU])[^aeiouAEIOU]', test_str))
 
#printing result
print("Characters around vowels count : " + str(res))


Output

The original string is :neveropenforneveropen
Characters around vowels count : 10

Time Complexity: O(n)

Auxiliary Space: O(n)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS