We Got the Address! 21/100 Days of Code
My latest feature finds the address containing the zipcode entered by a user.
This allows for obtaining the street number, street name, and properly removing it from the list when finished.
The address should be at the beginning of the list, the end, or somewhere in the middle. Finding it is made simple by the fact that each address is separated by commas.
If the target address is at the beginning of the list, it should have a comma after the zipcode. Since the zipcode is eight characters long, the index of the first comma should be eight more than that of the zipcode.
If the target address is at the end of the list, it will appear right after the last comma in the list. The zipcode should also appear eight characters before the end of the list.
All other addresses are considered to be in the middle of the list. Once the first instance of the zipcode occurs, I can create a temporary list to obtain the address.
By taking a substring of the original list from the beginning to eight characters after the index of the zipcode, the address becomes the last address in the temporary list. From there, I can reuse the same code for locating an address at the end of the list.
I used a switch statement and put the case for middle addresses above the one for end addresses. The only thing the middle case does is create the new aforementioned list.
Once the new list is created, the program can easily obtain the address by falling down to the end case. Not only does the program obtain the middle address, it does so without changing the original list (coming soon).
-CF