DEV Community

Keerti Sadana S
Keerti Sadana S

Posted on Edited on

PYTHON - STRING MANIPULATION (COUNT)

1) Count of Each Letter in a given String

name = 'keertisadana'
lst = list(name)
i = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*':
        print(key , 'is present', count,' times')
    i+=1 
Enter fullscreen mode Exit fullscreen mode

Output:
k is present 1 times
e is present 2 times
r is present 1 times
t is present 1 times
i is present 1 times
s is present 1 times
a is present 3 times
d is present 1 times
n is present 1 times

2) Duplicate letters

name = 'keertisadana'
lst = list(name)
i = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count > 1:
        print(key , ' is present',count,' times')
    i+=1 
Enter fullscreen mode Exit fullscreen mode

Output:
e is present 2 times
a is present 3 times

3) Non-duplicate letters

name = 'keertisadana'
lst = list(name)
i = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count == 1:
        print(key , ' is present',count,' times')
    i+=1 
Enter fullscreen mode Exit fullscreen mode

Output:
k is present 1 times
r is present 1 times
t is present 1 times
i is present 1 times
s is present 1 times
d is present 1 times
n is present 1 times

4) Duplicate letters Count

name = 'keertisadana'
lst = list(name)
i = 0
dup = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count > 1:
        dup+=1
    i+=1 
print(dup , 'duplicates letters')
Enter fullscreen mode Exit fullscreen mode

Output:
2 duplicates letters

5) Non-duplicate letters Count

name = 'keertisadana'
lst = list(name)
i = 0
n_dup = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count == 1:
        n_dup+=1
    i+=1 
print(n_dup , 'non-duplicate letters')
Enter fullscreen mode Exit fullscreen mode

Output:
7 non-duplicate letters

6) All letters are unique or not

word = 'keertisadana'
n_dup = 0
dup = 0
lst = list(word)
i = 0
while i < len (lst):
    key = lst[i]
    count = 1
    j = i+1
    while j<len(lst):
        if key == lst[j] and key != '*':
            lst[j] = '*'
            count+=1
        j+=1
    if key != '*' and count > 1:
        n_dup += 1
    i+=1
if len(word) == n_dup:
    print('All letters are unique')
else:
    print('All letters are not unique')
Enter fullscreen mode Exit fullscreen mode

Output:
All letters are not unique

7) Most Frequent letter

word = 'keertisadana'
lst = list(word)
max_count = 0
most_frequent = ' '
i = 0
while i < len (lst):
    key = lst[i]
    count = 1
    j = i+1
    while j<len(lst):
        if key == lst[j] and key != '*':
            lst[j] = '*'
            count+=1
        j+=1
    if key != '*' and count > max_count:
        max_count = count
        most_frequent = key
    i+=1
print(f"The most frequent letter is '{most_frequent}' appearing {max_count} times.")
Enter fullscreen mode Exit fullscreen mode

Output:
The most frequent letter is 'a' appearing 3 times.

8) First Repeated Character

name = 'keertisadana'
lst = list(name)
i = 0
dup = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count > 1:
        dup+=1
        break
    i+=1
print(f'first repeated character is {key}')
Enter fullscreen mode Exit fullscreen mode

Output:
first repeated character is e

9) First Non-Repeated Character

name = 'keertisadana'
lst = list(name)
i = 0
n_dup = 0
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            #lst[j] = '*'
        j+=1
    if key != '*' and count == 1:
        n_dup+=1
        break
    i+=1
print(f'first non_repeated character is {key}')
Enter fullscreen mode Exit fullscreen mode

Output:
first non_repeated character is k

10) Last Non-Repeated Character

name = 'keertisadana'
lst = list(name)
i = 0
n_dup = ''
last_rep_letter = ' '
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count == 1:
        n_dup+=key        
    i+=1
print(f'last non_repeated character is {n_dup[-1]}')
Enter fullscreen mode Exit fullscreen mode

Output:
last non_repeated character is n

11) Last Repeated Character

name = 'keertisadana'
lst = list(name)
i = 0
dup = ''
last_rep_letter = ' '
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if key != '*' and count > 1:
        dup+=key        
    i+=1
print(f'last repeated character is {dup[-1]}')
Enter fullscreen mode Exit fullscreen mode

Output:
last repeated character is a

12) Last Repeated Vowel

name = 'keertisadana'
lst = list(name)
i = 0
vowel = ''
last_rep_letter = ' '
while i < len(lst):
    count = 0
    key = lst[i]
    j = i
    while j < len(lst):
        if key == lst[j] and key != '*':
            count+=1
            lst[j] = '*'
        j+=1
    if (key != '*' and key in 'aeiou') and count > 1:
        vowel+=key        
    i+=1
print(f'last repeated vowel is {vowel[-1]}')
Enter fullscreen mode Exit fullscreen mode

Output:
last repeated vowel is a

Top comments (0)