Finding And Displaying Specific Characters From An Array
2 min readMay 11, 2023
ALGORITHM:
- Initialize an empty string called “a” and a character called “char”.
- Initialize an empty string called “result”.
- Initialize two integer variables, “i” and “count”, both set to 0.
- Loop through the array “a” and increment the “count” variable each time you encounter an element in the array.
- Initialize another integer variable “j” to 0.
- Loop through the array “a” and compare each element to the “char” variable.
- If the character not matches with an element in the array, concatenate that character with the “result” string.
- Increment the “j” variable.
- Repeat the loop until all elements in the array have been compared.
- Print the “result” string.
DETAILED EXPLANATION:
- In this step, we set the input string “a” to an empty string and the search character “char” to the character we want to search for. For example, if we wanted to search for all instances of the letter “a” in the word “australia”, we would set “a” to “australia” and “char” to “a”.
- Here we initialize the output string “Result” to an empty string. This is where we’ll store all the matching characters we find in step 6.
- In this step, we initialize a counter variable “i” and a count variable “count” to 0. “i” will be used to keep track of our position in the input string “a”, while “count” will keep track of the total number of characters in “a”.
- We loop through each character in the input string “a” and increment the “count” variable for each character in the string. This gives us the total number of characters in “a”.
- Here we initialize a second counter variable “j” to 0. We’ll use “j” to keep track of our position in the output string “Result” as we add matching characters.
- We loop through the input string “a” again, and for each character that not matches the search character “char”, we add that character to the “Result” string. This is done by checking if the character at position “i” in “a” not matches the search character “char”. If it does, we add that character to the “Result” string and increment “j” to keep track of our position in “Result”.
- Once we’ve looped through all the characters in “a”, we print out the final value of “Result”.
CONCLUSION:
With this algorithm, you can easily extract specific characters from a string in any programming language. This algorithm is easy to understand and implement for beginners.
FLOWCHART: