Michael Wilson
Michael Wilson

Reputation: 11

Bool function to check for a specific string requirement

I'm currently writing a program in which I must write a bool function that determines if a set of directions are valid input or not. The directions must be in the form of a number followed by a direction (N W E S). For example, a valid input would be 4N5S2W7E. I am totally lost on how to use the bool function to test if an input is valid or not. There may be no spaces and each number must be followed by a letter direction (and each letter direction must be preceded by a number). Any help is much appreciated. Thanks!

Upvotes: 1

Views: 322

Answers (1)

Luke Xu
Luke Xu

Reputation: 2440

Well your function is only going to be part of the main function so for example

static bool CheckDirection(string s){
   // loop through each character in the string
   // check if char is an a-z and A-Z or if it's 0-9
   // compare next char and previous char
   // I'll let you figure out the exact logic here


    if (it is valid)
        return true
    else
        return false // If it's not right
}

Then in your main function you simply call

int main(){

    if(CheckDirection(string s)){

    }

return 0
}

Since the CheckDirection function is going to return a bool. You can just place it into the if statement directly.

Upvotes: 1

Related Questions