I'm just learning Java and I have an assignment where I need to translate a snippet of C to Java. Can anybody help me by translating the snippet to pseudocode? I want to do the Java coding on my own, but I do not know C and I can't get much sense out of the snippet. This is the assignment:
You are looking for a simple pattern matching method. Similar to strstr (...) in C, it should search for a search string in a string. The search string should contain "*" (replacement for several characters) and "?". You have an example, but it's in C:
int match ( char *pat, char *str ) {
switch ( *pat ) {
case '\0' : return !*str;
case '*' : return match( pat+1, str ) || *str && match( pat, str+1 );
case '?' : return *str && match( pat+1, str+1 );
default : return *pat == *str && match( pat+1, str+1 );
}
}
Translate it to Java.
I know it's stupid to try an assignment where you need to translate from a language you do not know and I cannot understand why this assignment is included in a list of Java learning tasks but I have to solve it and it would be very kind if anybody is willing to help me.
I've tried commenting it for you. Take a read and see if it helps you understand :).
/* Returns an integer (nonzero if the strings match, zero if they don't).
* - pat: A string (char *) which is your pattern.
* - str: A string (char *) which is your source string.
* Note: All strings end in the null-character ('\0') which has integer value zero.
*/
int match ( char *pat, char *str ) {
/* The switch extracts the first character of the string "pat" (*pat).
* Then, it will run the code in the case to which that character matches:
*/
switch ( *pat ) {
/* If you match null-character, then return nonzero only if the value
* of the leading character in str (*str) is zero (!*str) This means
* that it returns a match if the leading character in str is also
* the null character
*/
case '\0' : return !*str;
/* If you match an asterisk '*', then return nonzero in two cases:
* (1) Calling your own function recursively having dropped the asterisk from
* the pattern returns nonzero (match(pat+1, str)).
* ... OR ...
* (2) The leading character of str is nonzero (*str) AND calling
* this very function having dropped the leading character of str returns
* nonzero (match(pat, str + 1)).
*/
case '*' : return match( pat+1, str ) || *str && match( pat, str+1 );
/* If you match '?', then return nonzero if both cases are true:
* (1) *str is not the null-char (it is nonzero).
* (2) Calling match recursively having skipped the current character
* in both the pattern AND the string returns nonzero (match(pat+1, str+1)).
*/
case '?' : return *str && match( pat+1, str+1 );
/* Otherwise, if you didn't match on any of the above patterns, return
* nonzero only if both the following conditions are true:
* (1) The current character at the head of pat is the same as that of str
* (*pat == *str)
* (2) calling match recursively having skipped both the current character
* at the head of pattern AND the string also returns nonzero.
* match(pat + 1, str + 1)
*/
default : return *pat == *str && match( pat+1, str+1 );
}
}