Chapter-3/3-3/expand.c
/* expand:  expands shorthand notations in the string s1 into the
            equivalent complete list in s2 */
expands shorthand notations in the string s1 into the
equivalent complete list in s2
expands shorthand notations in the string s1 into the equivalent complete list in s2
void expand(char s1[], char s2[]) {
  int i, j;
  char tmp;
  int dash;
  dash = 0;
  for (i = j = 0; s1[i] != '\0'; ++i) {
    if (s1[i] == '-') {
      if (i == 0 || s1[i + 1] == '\0') {
        s2[j++] = s1[i];
      } else {
        if (validrange(s1[i - 1], s1[i + 1])) {
          while (s2[j - 1] < s1[i + 1]) {
            s2[j] = s2[j - 1] + 1;
            j++;
          }
          ++i;
        } else {
          s2[j++] = s1[i];
        }
      }
    } else {
      s2[j++] = s1[i];
    }
  }
  s2[j] = '\0';
}
void expand ( char s1 [ ] , char s2 [ ] ) { int i , j ; char tmp ; int dash ; dash = 0 ; for ( i = j = 0 ; s1 [ i ] != ' \0 ' ; ++ i ) { if ( s1 [ i ] == ' ' ) { if ( i == 0 || s1 [ i + 1 ] == ' \0 ' ) { s2 [ j ++ ] = s1 [ i ] ; } else { if ( validrange ( s1 [ i - 1 ] , s1 [ i + 1 ] ) ) { while ( s2 [ j - 1 ] < s1 [ i + 1 ] ) { s2 [ j ] = s2 [ j - 1 ] + 1 ; j ++ ; } ++ i ; } else { s2 [ j ++ ] = s1 [ i ] ; } } } else { s2 [ j ++ ] = s1 [ i ] ; } } s2 [ j ] = ' \0 ' ; }
expand:  expands shorthand notations in the string s1 into the
equivalent complete list in s2
expand : expands shorthand notations in the string s1 into the equivalent complete list in s2
/* '-' is leading or trailing, so just copy it. */ /* check if this is a valid range. */ /* skip next character we already printed it. */ /* End the string with a NUL byte */