Thread: K&R 3-3 expand function

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    K&R 3-3 expand function

    Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z.

    Could someone please give me some constructive criticism in regards to my code? I have looked at the way other people did it as well as the solution at the end of the book which was nothing like what I managed to come up with.

    Code:
    void expand (char s1[], char s2[]){
     
      int i = j = 0, q;
     
     
      while(s1[i] != '\0')
      {
        if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-') //1st or last char is a "-"
          {  
              s2[j++] = s1[i];
           else if(s1[i] == '-')
           {
                if(s1[i-1] >= '0' && s1[i-1] <= '9')  // check if the left char is a 0 or a 9 eg. 0-9, 9-0
                {
                    if(s1[i+1] >= '0' && s1[i+1] <= '9')  //check the same for the right char
                    {
                       if(s1[i-1] <= s1[i+1])
                       {
                         for(q = s1[i-1]; q <= s1[i+1]; q++)
                           s2[j++] = q;
                       }
                    
                    }
    
                    else if(s1[i-1] == islower() || s1[i-1] == isupper() // check if characters are digits
                         || s1[i+1] == islower || s1[i+1] == isupper())
                    printf("Mismatching arguements unable to expand.");
                    else s2[j++] = s1[i];   
                } 
                elseif(s1[i-1] == islower() || s1[i-1] == islower())
                {
                    if(s1[i+1] == islower() || s1[i+1] == islower())
                    {
                       if(s1[i-1] <= s1[i+1])
                       { 
                          for(q = s[i-1];q = s[i+1]; q++)
                              s2[j++] = q;
                       }
                    }
                    else if(s1[i-1] == isdigit() || s1[i-1] == isupper() // check if characters are lowercase
                         || s1[i+1] == isdigit || s1[i+1] == isupper())
                    printf("Mismatching arguements unable to expand.");
                    else s2[j++] = s1[i];
                }
                elseif(s1[i-1] == isupper() || s1[i-1] == isupper())
                {
                    if(s1[i+1] == isupper() || s1[i+1] == isupper())    
                    { 
                       if(s1[i-1] <= s1[i+1])
                       {
                         for(q = s[i-1];q = s[i+1]; q++)
                             s2[j++] = q;
                       }
                    }
                    else if(s1[i-1] == isdigit() || s1[i-1] == islower() //check if characters are uppercase
                         || s1[i+1] == isdigit || s1[i+1] == islower())
                    printf("Mismatching arguements unable to expand.");
                    else s2[j++] = s1[i];
                }
                else s2[j++] = s1[i];
          }
          else if( s1[i+1] == '-') // check if there is a '-' at the end
          {
               if(s1[i] >= isdigit() || s1[i] >=isupper() || s1[i] >= islower())
               ;
               else s2[j++] = s1[i]
          }
          else if(s1[i-1] !=  '-' && s1[i+1] != '-') //check if there is a '-' at the start
               s2[j++] = s1[i];
       
          i++;
          }
       s2[j] == '\0';
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-') //1st or last char is a "-"
          {  
              s2[j++] = s1[i];
          } /* Missing brace */                      
    
          else if(s1[i] == '-')
          {
    Fact - Beethoven wrote his first symphony in C

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Do you have code that compiles?

    As click has mentioned, you're missing braces. And your indentation is presumably incorrect. if/else if's should line up like this:
    Code:
    if (...) {
    }
    else if (...) {
    }
    else if (...) {
    }
    else {
    }
    Line 3 should be
    int i = 0, j = 0, q;

    Line 30 (or is it 31?) should have a space between the if and the else. Same with the other elseif's.

    Line 74 should be
    s2[j] = '\0';
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed to expand sin(x)
    By rajarshi in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 03:19 AM
  2. Expand nightmare!
    By zach48191 in forum C Programming
    Replies: 9
    Last Post: 10-18-2011, 11:07 PM
  3. Expand
    By ASCII in forum C Programming
    Replies: 11
    Last Post: 10-03-2011, 04:01 AM
  4. Expand an array
    By SlyVixsky in forum C Programming
    Replies: 9
    Last Post: 08-01-2009, 11:28 PM
  5. expand(int s[]) 3-3 knr
    By olbas in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 11:01 AM