Hey, Ive been trying to work on this problem for at least a couple hours but still am not sure how to approach it. I need to write a bool function with string path with the following requirements:
-------------------------------------------------------------------------------
Let's define a path segment as one or two digit characters followed by one of the four letters N, E, S, or W, in either upper or lower case. Two examples are 2N and 13e. A path is a sequence of zero or more path segments; an example is 2N1e1E2s1e.
bool isPathWellFormed(string path)
This function returns true if its parameter is a syntactically valid path, and false otherwise. A syntactically valid path is a sequence of zero or more path segments (not separated by spaces, commas, or anything else). Here are two syntactically valid paths: 2N1e01E0n2e1e and 42W. Here are four strings that are not syntactically valid paths: s3n, 1ex, 144N, and 2w+3n.
------------------------------------------------------------------------------
Not asking for anyone to write out the whole code for me just wondering if anyone had any ideas on where to start?
Yeah, it sounds like you would have to check each character and compare it to adjacent characters. If you ever have a letter that is not N, E, S, or W, the bool is false. If you ever have two adjacent letters, false. More than 2 numbers next to each other, false.
I would try a simple loop that adds each character to a string until it reaches a letter. that would be 1 path.
if the string is more than 3 characters long, not valid
if the every other character (not the letter) in the string is not a number, not valid
if the string's size (string_handle.size()) is less than 1 (the size is equal to 1), not valid
since we loop until a letter, we do not need to have a conditional statement for that.
And we would just do that until the end of the path and if it returns false at any point, your function can return false. This would work for any length path. Of course, this method would be a lot easier if you created bool functions that would recognize letters, numbers, and special characters.
now write it in C++. ;)