#include <stdio.h>
#include <string.h>
/* test[13] can be any length character array used for the search */
char test[13] = "Hello World!\0";
/*
*
* getchr is the search function which compares c to a for a
* possible match. It initialises c and a to unsigned char.
*
*/
char getchr(unsigned char c, unsigned char a);
/* end[1] is a constant and must not be changed.
* It is used to recognise the end of a string.
* All strings must be NULL terminated in the test[] string.
*/
const char end[1] = "\0";
int main() {
/* The search pattern. Can only be an unsigned char
* of length c[1].
* Used by getchr()
*/
char c[1] = " ";
/* int i is a local scope variable used by the while loop
* to cycle through the string test[i].
*/
int i = 0;
/* The while loop - compares end[0] ('\0') to test[i]
* to see if the end of the string has occurred.
*/
while ((unsigned char) end[0] != (unsigned char) test[i]) {
/* Call to getchr with c[0] and test[i]
* to test whether the characters match.
* Possibility to change getchr to return 0 on Match
* and >0 on False. At the moment it prints out
* 'Match' on finding a matching character pair.
*/
getchr((unsigned char) c[0],(unsigned char) test[i]);
/* Increment i by 1 to cycle through test[i] string. */
i++;
}
/* Exit the program with success. */
return 0;
}
/* Subroutine getchr
* Outputs 'Match' on character pair matching of unsigned char c and a.
* Used in main() while loop to check for a character Match.
*/
char getchr(unsigned char c, unsigned char a) {
/* If unsigned char c equals exactly unsigned char a
* then print out 'Match' to stdout.
*/
if((unsigned char) c == (unsigned char) a){
/* Print output 'Match' on match to stdout (usually console) */
printf("Match\n\0");
}
}