Advertisement
levartolona

C_PRG_LANG_EX_3.3

Jan 31st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. int expand(char *s1, char *s2)
  7. {
  8.     while (!isdigit(*s1) && !isalpha(*s1))
  9.     {
  10.         *s2 = *s1;
  11.         s1++;
  12.         s2++;
  13.     }
  14.  
  15.     while(*s1)
  16.     {
  17.         if (!(*s2 = *s1)) // End of line
  18.             return 0;
  19.  
  20.         char start = *s1;
  21.         if (*(s1 + 1) == '-')
  22.         {
  23.             s1 += 2;
  24.             if (!(*s1)) // End of line
  25.             {
  26.                 *s2++ = start;
  27.                 *s2++ = '-';
  28.                 *s2 = *s1;
  29.                 return 0;
  30.             }
  31.             if (start <= *s1 && ((isdigit(*s1) && isdigit(start)) ||
  32.                 (isalpha(*s1) && isalpha(start))))
  33.             {
  34.                 while (start <= *s1)
  35.                 {
  36.                     *s2++ = start;
  37.                     start++;
  38.                 }
  39.                 s1++;
  40.             }
  41.             else
  42.                 return -1;  // Incorrect sequence
  43.         }
  44.         else
  45.             *s2++ = *s1++;
  46.     }
  47.     *s2 = *s1;
  48.     return 0;
  49. }
  50.  
  51. int main(void)
  52. {
  53.     char s1[256] = "a-z";
  54.     char s2[256];
  55.  
  56.     if (expand(s1, s2))
  57.     {
  58.         printf("Incorrect sequence!\n");
  59.         return -1;
  60.     }
  61.     printf("%s\n", s1);
  62.     printf("%s\n\n", s2);
  63.  
  64.     strcpy(s1, "0-9");
  65.     if (expand(s1, s2))
  66.     {
  67.         printf("Incorrect sequence!\n");
  68.         return -1;
  69.     }
  70.     printf("%s\n", s2);
  71.     printf("%s\n\n", s2);
  72.  
  73.     strcpy(s1, "a-z0-9");
  74.     if (expand(s1, s2))
  75.     {
  76.         printf("Incorrect sequence!\n");
  77.         return -1;
  78.     }
  79.     printf("%s\n", s2);
  80.     printf("%s\n\n", s2);
  81.  
  82.     strcpy(s1, "a-z0-9-");
  83.     if (expand(s1, s2))
  84.     {
  85.         printf("Incorrect sequence!\n");
  86.         return -1;
  87.     }
  88.     printf("%s\n", s2);
  89.     printf("%s\n\n", s2);
  90.  
  91.     strcpy(s1, "a-z qwerty 0-9-");
  92.     if (expand(s1, s2))
  93.     {
  94.         printf("Incorrect sequence!\n");
  95.         return -1;
  96.     }
  97.     printf("%s\n", s2);
  98.     printf("%s\n\n", s2);
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement