Write a function escape(s, t) that converts characters like newline and tab int visible escape sequences like \n and \t as it copies the string t to s.Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters.

/*escape: expand newline and tab into visible sequences */
/*  while copying the string t to s   */
void escape(char s[], char t[]){
      switch(t[i]){
         case '\n':
              s[j++] = '\\';
              s[j++] = 'n';
              break;
        case '\t':
              s[j++] = '\\';
              s[j++] = 't';
              break;
        default:
              s[j++] = t[i];    /* all other chars */
              break;
       }
       s[j] = '\0';
}

/*unescape: convert escape sequences int real characters */
/*    while copying the string t to s          */
void unescape(char s[], char t[]){
      int i, j;
      for(i = j= 0; t[i] != '\0'; i++)
            if (t[i] != '\\')
                s[j++] = t[i];
            else                   /* it is a backslash */
               switch(t[++i]){
                   case 'n':     /* real newline */
                         s[j++] = '\n';
                         break;
                   case 't':        /*real tab */
                          s[j++] = '\t';
                          break;
                   default:
                          s[j++] = '\\';
                          s[j++] = t[i];
                          break;
                 }
                s[j] = '\0';
}

Leave a comment