Advertisement
cd62131

transform string

Nov 29th, 2018
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. static char t(char);
  6. static char t(char c) {
  7.     static const char *tab[] = { "aehiouwy", "bfpv", "cgjkqsxz", "dt", "l", "mn", "r" };
  8.     if (!islower(c)) {
  9.         return c;
  10.     }
  11.     for (size_t i = 0; i < sizeof tab / sizeof tab[0]; ++i) {
  12.         if (strchr(tab[i], c)) {
  13.             return '0' + i;
  14.         }
  15.     }
  16.     // NOTREACHED
  17.     return '\0';
  18. }
  19. int main(void) {
  20.     char buf[BUFSIZ];
  21.     if(!fgets(buf, BUFSIZ, stdin)) {
  22.         goto end;
  23.     }
  24.     int len = strlen(buf);
  25.     if (buf[len - 1] == '\n') {
  26.         buf[len - 1] = '\0';
  27.         --len;
  28.     }
  29.     if (len < 1) {
  30.         goto end;
  31.     }
  32.     for (int i = 0; i < len; ++i) {
  33.         if (!isalpha(buf[i])) {
  34.             goto end;
  35.         }
  36.     }
  37.     char *p = buf;
  38.     char *prev = NULL;
  39.     do {
  40.         if (strchr("aeiouy", *p)) {
  41.             prev = p;
  42.             continue;
  43.         }
  44.         if (strchr("hw", *p)) {
  45.             continue;
  46.         }
  47.         if (!prev || t(*prev) != t(*p)) {
  48.             printf("%c", t(*p));
  49.         }
  50.         prev = p;
  51.     } while (*++p);
  52.     puts("");
  53.     exit(0);
  54. end:
  55.     exit(1);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement