1. Counting the Occurrence of the First Character
First, let's take a string and find how many times its first character appears.
name = 'ajay'
first_letter = name[0]
count = 1
i = 1
while i < len(name):
if first_letter == name[i]:
count += 1
i += 1
print(count)
Output:
2
Explanation:
- name[0] gets the first character, which is a.
- count = 1 because we already have one occurrence of a.
- The while loop starts from index 1.
- Each character is compared with the first character.
- If a match is found, the count is increased.
In ajay, the character a appears 2 times.
2. Counting the Occurrence of Each Character
Now, instead of checking only the first character, let's find the occurrence of every character in the string.
For this, we convert the string into a list and use * to mark characters that have already been counted.
s = 'ajay'
name = list(s)
letter = name[0]
count = 1
i = 1
if letter != '*':
while i < len(name):
if letter == name[i]:
name[i] = '*'
count += 1
i += 1
print(letter, 'appeared for', count, 'time(s)')
Output:
a appeared for 2 time(s)
Here, when another a is found, it is replaced with *. This helps us avoid counting it again.
We can repeat the same logic for the remaining indexes to count every character.
3. Counting All Character Occurrences Using Nested While Loops
Instead of writing separate code for every index, we can use two while loops.
s = 'mahendrasinghdhoni'
name = list(s)
j = 0
while j < len(name):
letter = name[j]
count = 1
i = j + 1
if letter != '*':
while i < len(name):
if letter == name[i]:
name[i] = '*'
count += 1
i += 1
print(letter, 'appeared for', count, 'time(s)')
j += 1
Explanation:
- The outer while loop selects each character.
- letter = name[j] stores the current character.
- count = 1 counts the current character itself.
- The inner while loop checks the remaining characters.
- When a matching character is found, it is replaced with *.
- The count is increased for every matching character.
- The outer loop skips characters already marked with *.
This program prints the occurrence of every distinct character.
4. Printing Only Repeated Characters
Sometimes, we don't need to print characters that appear only once. We only want characters that occur more than once.
We can achieve this by adding the condition count > 1.
s = 'mahendrasinghdhoni'
name = list(s)
j = 0
while j < len(name):
letter = name[j]
count = 1
i = j + 1
if letter != '*':
while i < len(name):
if letter == name[i]:
name[i] = '*'
count += 1
i += 1
if count > 1:
print(letter, 'appeared for', count, 'time(s)')
j += 1
Explanation:
The important condition is:
if count > 1:
It checks whether a character appears more than once.
- If the count is greater than 1, it is a repeated character.
- If the count is 1, it is not printed.
5. Finding the Number of Unique Characters
A unique character is a character that appears exactly once in a string.
For example:
s = 'mahendrasinghdhoni'
We can use the same logic and check whether count == 1.
s = 'mahendrasinghdhoni'
unique_count = 0
name = list(s)
j = 0
while j < len(name):
letter = name[j]
count = 1
i = j + 1
if letter != '*':
while i < len(name):
if letter == name[i]:
name[i] = '*'
count += 1
i += 1
if count == 1:
print(letter, 'appeared for', count, 'time(s)')
unique_count += 1
j += 1
print("No. of Unique Letters is", unique_count)
Explanation:
- unique_count = 0 initializes the unique character counter.
- Each character is checked against the remaining characters.
- If the count is exactly 1, it is a unique character.
- unique_count += 1 increases the number of unique characters.
At the end, we print the total number of unique characters.
6. Finding the Last Unique Character
Finally, let's find the last unique character in a string.
s = 'ajay'
unique_count = 0
last_unique_letter = ' '
name = list(s)
j = 0
while j < len(name):
letter = name[j]
count = 1
i = j + 1
if letter != '*':
while i < len(name):
if letter == name[i]:
name[i] = '*'
count += 1
i += 1
if count == 1:
print(letter, 'appeared for', count, 'time(s)')
last_unique_letter = letter
unique_count += 1
j += 1
print(last_unique_letter)
Output:
j appeared for 1 time(s)
y appeared for 1 time(s)
y
Explanation:
- last_unique_letter = ' ' initializes a variable to store the last unique character found.
- Whenever a character appears exactly once, we update last_unique_letter.
- The loop continues until all characters are checked.
- Since the variable is updated each time a unique character is found, it contains the last unique character after the loop finishes.
For the string ajay, the unique characters are j and y. Therefore, the last unique character is y.
Top comments (0)