Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Задание 3-3
- // Напишите программу, которая бы разворачивала сокращенную запись наподобие a-z в
- // строке s1 в полный список abc...xyz в строке s2, а так же запись z-a в zyx…cba. Программа
- // должна работать с буквами в любом регистре и цифрами
- #include <stdio.h>
- #include <stdlib.h>
- // this time 100 may be not enough
- #define str_maxlen 1000
- typedef char* str;
- str str_new() {
- return (str) malloc(sizeof(char) * (str_maxlen + 1));
- }
- str str_read(char end) {
- str s = str_new();
- str c = s;
- while ((*c = getchar()) != end && *c != EOF)
- c++;
- *c = EOF;
- return s;
- }
- str str_nulled(str old) {
- // c89/printf: "%s": The argument shall be a pointer to an array of character type.
- // Characters from the array are written up to (but not including) a terminating null character;
- // if the precision is specified, no more than that many characters are written.
- // If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
- // NOTE: EOF gets printed somehow ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- str s = str_new();
- str c = s;
- while (*old != EOF && *old != '\0')
- *(c++) = *(old++);
- *c = '\0';
- return s;
- }
- int charGroup(char c) {
- if (c >= '0' && c <= '9')return 0;
- if (c >= 'a' && c <= 'z')return 1;
- if (c >= 'A' && c <= 'Z')return 2;
- return -1;
- }
- str str_fill_a_z(str old) {
- str s = str_new();
- str c;
- char z;
- for (c = s; *old != EOF; old++) {
- if (charGroup(*old) > -1 &&
- *(old + 1) == '-' &&
- charGroup(*old) == charGroup(*(old + 2)) ) {
- if (*old <= *(old + 2)) {
- for (z = *old; z <= *(old + 2); z++)
- *(c++) = z;
- } else {
- for (z = *old; z >= *(old + 2); z--)
- *(c++) = z;
- }
- old += 2;
- } else
- *(c++) = *old;
- }
- *c = EOF;
- return s;
- }
- int main() {
- str s1 = str_read('\n'); // reads to newline! EOF is not needed!
- str s2 = str_fill_a_z(s1);
- printf("\"%s\"\n", str_nulled(s2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement