Regular Expressions (Lecture 11)

  • Current readings King Chap. 17 or K&R Chapter 6, Structures.
  • The book "Your Unix" has good chapters on vi and emacs, pattern matching (Chapter 15) and regular expressions, CVS and make.

  • Press here for other lectures
  • Press here for slides #11 (postscript) regular expressions and pointers to functions (old)

  • Press here for basic information about Structures
  • Press here for next design topic from Mark Green's notes.
  • Preparations for C++ is also worth reading at this time.

    Fun with strings


    The assumption that a string ends with the null character permits writing very concise programs for manipulation of existing strings. Usually we need dynamic memory allocation to create room for a new string (e.g. malloc works well here).

    Let us examine four ways of how we can copy strings. These functions have the following preconditions:

       /* Array subscript version */
    void astrcopy (char s[], char t[]) {	// or (char* s, char* t)
      int i = 0;
      while ((t[i] = s[i]) != '\0')
        i++;
    }
    
       /* Pointer version 1 */
    void p1strcopy (char* s, char* t) {
      int i = 0;
      while ((*(t+i) = *(s+i)) != '\0') 
        i++;
    }
    
       /* Pointer version 2 */
    void p2strcopy (char* s, char* t) {
      while ((*t++ = *s++) != '\0') 
        ;
    }
    
       /* Pointer version 3 */
    void p3strcopy (char* s, char* t) {
      while (*t++ = *s++)  
        ;
    }