//PAC: things with strings //jleblanc: 2.27.06 /*Along the way, investigate the library "strings.h", as the functions there could be quite interesting. For example, write a function that determines whether a string is a candidate e-mail address ... or html file ... or txt file. In addition, consider how you can create a new version of the queue program, such that the program handles strings rather than characters.*/ #include #include #include //AN email would be: // _@_._ where last space either: edu, com, net, org, gov, .... But that is not safe int is_email(char word[]); void main() { char word[50]; //char *a, *d; char word1[]="jleblanc@nyu.edu"; char word2[]="jleblanc.htm"; char word3[]="jleblanc.html"; char word4[]="jlebl@anc@nyu.ed.u";//A problem kind of function printf("enter a word: "); scanf("%s", word); printf("\n%s\n\n", word); printf("\n%d\n", is_email(word1)); printf("\n%d\n", is_email(word2)); printf("\n%d\n", is_email(word3)); printf("\n%d\n\n", is_email(word4)); printf("\n%d\n", is_htm(word1)); printf("\n%d\n", is_htm(word2)); printf("\n%d\n", is_htm(word3)); printf("\n%d\n\n", is_htm(word4)); } //Very Crude Function int is_email(char word[]) { int is_it=-1; char *a, *d; a=strchr(word, '@'); if(a!=NULL) {d=strchr(a, '.');} if(a!=NULL && d!=NULL) { is_it=1; } return is_it; } //Very Crude Function //This might pick up html pages too int is_htm(char word[]) { int is_it=-1; char *h; h=strstr(word, ".htm"); if(h!=NULL) { is_it=1; } return is_it; } /* 2.14.9 strchr Declaration: char *strchr(const char *str, int c); Searches for the first occurrence of the character c (an unsigned char) in the string pointed to by the argument str. The terminating null character is considered to be part of the string. Returns a pointer pointing to the first matching character, or null if no match was found. */ /*2.14.21 strstr Declaration: char *strstr(const char *str1, const char *str2); Finds the first occurrence of the entire string str2 (not including the terminating null character) which appears in the string str1. Returns a pointer to the first occurrence of str2 in str1. If no match was found, then a null pointer is returned. If str2 points to a string of zero length, then the argument str1 is returned */