Jan 3, 2012

K & R C Programs Exercise 3-3.

K and R C, Solution to Exercise 3-3:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to expand short hand notations like a-z in the string s1 into the equivalent complete list abc---xyz in s2. Allowed for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arranged that a leading or trailing - is taken literally. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include 
#include 

void expand(char * s1, char * s2);

int main(void) {
 char *s[] = { "a-z-", "z-a-", "-1-6-",
   "a-ee-a", "a-R-L", "1-9-1",
   "5-5", NULL };
 char result[100];
 int i = 0;

 while ( s[i] ) {

  expand(result, s[i]);
  printf("Unexpanded: %s\n", s[i]);
  printf("Expanded  : %s\n", result);
  ++i;
 }

 return 0;
}



//expand: expand short hand notations in s1 into string s2 
void expand(char * s1, char * s2) {
 static char upper_alph[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 static char lower_alph[27] = "abcdefghijklmnopqrstuvwxyz";
 static char digits[11]     = "0123456789";

 char * start, * end, * p;
 int i = 0;
 int j = 0;


 /*  Loop through characters in s2  */

 while ( s2[i] ) {
  switch( s2[i] ) {
  case '-':
   if ( i == 0 || s2[i+1] == '\0' ) {

    s1[j++] = '-';
    ++i;
    break;
   }
   else {


    if ( (start = strchr(upper_alph, s2[i-1])) &&
      (end   = strchr(upper_alph, s2[i+1])) )
     ;
    else if ( (start = strchr(lower_alph, s2[i-1])) &&
      (end   = strchr(lower_alph, s2[i+1])) )
     ;
    else if ( (start = strchr(digits, s2[i-1])) &&
      (end   = strchr(digits, s2[i+1])) )
     ;
    else {

     fprintf(stderr, "EX3_3: Mismatched operands '%c-%c'\n",
       s2[i-1], s2[i+1]);
     s1[j++] = s2[i-1];
     s1[j++] = s2[i++];
     break;
    }


    p = start;
    while ( p != end ) {
     s1[j++] = *p;
     if ( end > start )
      ++p;
     else
      --p;
    }
    s1[j++] = *p;
    i += 2;
   }
   break;

  default:
   if ( s2[i+1] == '-' && s2[i+2] != '\0' ) {

    ++i;
   }
   else {

    s1[j++] = s2[i++];
   }
   break;
  }
 }
 s1[j] = s2[i];    
}


Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link