Advertisement
Dimava

3-3

Mar 27th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. // Задание 3-3
  2. // Напишите программу, которая бы разворачивала сокращенную запись наподобие a-z в
  3. // строке s1 в полный список abc...xyz в строке s2, а так же запись z-a в zyx…cba. Программа
  4. // должна работать с буквами в любом регистре и цифрами
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.                     // this time 100 may be not enough
  9. #define str_maxlen 1000
  10.  
  11. typedef char* str;
  12.  
  13. str str_new() {
  14.     return (str) malloc(sizeof(char) * (str_maxlen + 1));
  15. }
  16.  
  17. str str_read(char end) {
  18.     str s = str_new();
  19.     str c = s;
  20.     while ((*c = getchar()) != end && *c != EOF)
  21.         c++;
  22.     *c = EOF;
  23.     return s;
  24. }
  25.  
  26.  
  27. str str_nulled(str old) {
  28. // c89/printf: "%s": The argument shall be a pointer to an array of character type.
  29. //                   Characters from the array are written up to (but not including) a terminating null character;
  30. //                   if the precision is specified, no more than that many characters are written.
  31. //                   If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
  32. // NOTE: EOF gets printed somehow                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  33.     str s = str_new();
  34.     str c = s;
  35.     while (*old != EOF && *old != '\0')
  36.         *(c++) = *(old++);
  37.     *c = '\0';
  38.     return s;
  39. }
  40.  
  41. int charGroup(char c) {
  42.     if (c >= '0' && c <= '9')return 0;
  43.     if (c >= 'a' && c <= 'z')return 1;
  44.     if (c >= 'A' && c <= 'Z')return 2;
  45.     return -1;
  46. }
  47.  
  48. str str_fill_a_z(str old) {
  49.     str s = str_new();
  50.     str c;
  51.     char z;
  52.     for (c = s; *old != EOF; old++) {
  53.         if (charGroup(*old) > -1 &&
  54.                 *(old + 1) == '-' &&
  55.                 charGroup(*old) == charGroup(*(old + 2)) ) {
  56.             if (*old <= *(old + 2)) {
  57.                 for (z = *old; z <= *(old + 2); z++)
  58.                     *(c++) = z;
  59.             } else {
  60.                 for (z = *old; z >= *(old + 2); z--)
  61.                     *(c++) = z;
  62.             }
  63.             old += 2;
  64.         } else
  65.             *(c++) = *old;
  66.     }
  67.     *c = EOF;
  68.     return s;
  69. }
  70.  
  71. int main() {
  72.     str s1 = str_read('\n'); // reads to newline! EOF is not needed!
  73.     str s2 = str_fill_a_z(s1);
  74.     printf("\"%s\"\n", str_nulled(s2));
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement