strings and chars

closed account (Dy0fSL3A)
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?
Last edited on
So I'm guessing you would have to check through each character for a pattern.

So if say, '3N23WW4SE' that would be false correct?
closed account (Dy0fSL3A)
correct
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.
closed account (Dy0fSL3A)
I just came up with this, but even when I input a simple segment like 2n, the function returns false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
		
		if(	path[0] !='0' && path[1] !='1' && path[1] !='2' && path[1] !='3' && path[1] !='4' && path[1] !='5' && path[1] !='6' && path[1] !='7' && path[1] !='8' && path[1] !='9');
			return false;
		if (path[1] !='0' && path[1] !='1' && path[1] !='2' && path[1] !='3' && path[1] !='4' && path[1] !='5' && path[1] !='6' && path[1] !='7' && path[1] !='8' && path[1] !='9' || (path[1]!='e'&& path[1]!='w'&& path[1]!='s'&& path[1]!='n'&& path[1]!='E'&& path[1]!='W'&& path[1]!='S'&& path[1]!='N'))
			return false;
		else for (int k; k <= path.size(); k++)
		{
			if(isalpha(path[k]) && (isalpha(path[k-1]) || isalpha(path[k+1])))
			{
				return false;
			}
			else if(isdigit(path[k]) && (isdigit(path[k-1]) && isdigit(path[k-2])))
				return false;
			else if(isdigit(path[k]) && (isdigit(path[k+1]) && isdigit(path[k+2])))
				return false;
		}
		return true;
	}
Last edited on
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++. ;)
Last edited on
Topic archived. No new replies allowed.