2014年05月16日

来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/03/29 08:14

practice 3-3: Write a function expand(s1,s2) whichexpand the shorthand notation like a-z among the input string s1into a complete list form a to z like abc...xyz.Allow for lettersof either case and digits,and be  prepared tohandle  cases like a-b-c,a-z0-9,-a-z .Arrange that a leading and trailing - will be takenliterally.

#include
#define MAXLINE 1000
void expand(char s1[],char s2[])
{
 int i,j=0;
 //char c;
 for(i=0;s1[i]!='\0';)
 
  if((s1[i]>='a'&&s1[i]<='z')&&(s1[i+1]=='-')&&(s1[i+2]>='a'&&s1[i+2]<='z'))
  {
   s2[j]=s1[i];
   while(s2[j]<=s1[i+2])
   {
    s2[j+1]=s2[j]+1;
    ++j;
   }
   i=i+2;
  }
  elseif((s1[i]>='A'&&s1[i]<='Z')&&(s1[i+1]=='-')&&(s1[i+2]>='A'&&s1[i+2]<='Z'))
  {
   s2[j]=s1[i];
   while(s2[j]<=s1[i+2])
   {
    s2[j+1]=s2[j]+1;
    ++j;
   }
   i=i+2;
  }
  elseif((s1[i]>='0'&&s1[i]<='9')&&(s1[i+1]=='-')&&(s1[i+2]>='0'&&s1[i+2]<='9'))
  {
   s2[j]=s1[i];
   while(s2[j]<=s1[i+2])
   {
    s2[j+1]=s2[j]+'1'-'0';
    ++j;
   }
   i=i+2;
  }
  else
   s2[j]=s1[i];
   ++i;
 }
   
 
  s2[j]='\0';
    
  
}
main()
{
 charstr1[MAXLINE]="a-cA-C0-9",str2[MAXLINE];
 

 printf("orignal string:\n%s\n",str1);

 expand(str1,str2);

 printf("expand string:\n%s\n",str2);

 return 0;
}

 

CONCLUSION:This time i failed, half work is done,itcould handle like a-z,a-z0-9 exceptlike -a-z,a-c-z and so on,so i put the rightanswer here for reference.

#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 };
    charresult[100];
    int i =0;
   
    while ( s[i]) {
       
       
       
       expand(result, s[i]);
       printf("Unexpanded: %s\n", s[i]);
       printf("Expanded  : %s\n", result);
       ++i;
    }
   
    return0;
}


void expand(char * s1, char * s2) {
    static charupper_alph[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    static charlower_alph[27] = "abcdefghijklmnopqrstuvwxyz";
    static chardigits[11]    = "0123456789";
   
    char *start, * end, * p;
    int i =0;
    int j =0;
   
   
   
   
    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];   
}

 

0 0
原创粉丝点击