Counting The Occurrences Of A Character In An Array Algorithm
1 min readApr 27, 2023
ALGORITHM:
- Get the input array of characters, “a[]”.
- Get the input character to search, “char”.
- Set the initial count of occurrences to 0: “count=0”.
- Set the initial result to 0: “result=0”.
- Find the length of the array by iterating through the elements of the array until the end is reached. Set “count” to the total number of elements in the array.
- Set “i” to 0
- Repeat until the end of the array is reached:
- Increment “i” by 1
- Increment “count” by 1
6. Iterate through each element of the array:
- Set “j” to 0
- Repeat until the end of the array is reached:
- If the element at index “j” is equal to the character “char”, increment “result” by 1.
- Increment “j” by 1
7. Print the number of occurrences of the character in the array.
- Print “result”.
CONCLUSION:
The Algorithm “Counting The Occurrences Of A Character In An Array” takes an input array of characters and a character to search, and outputs the number of times the given character appears in the array.
FLOWCHART: