Advertisement
MonsterScripter

CodinGame_2023_08_22__15_17_00__n_chars.c

Aug 22nd, 2023
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. /**
  8.     Line 1: A sequence of two alphabetic characters. sep
  9.     Line 2: A sequence of alpha-numeric characters. serie
  10.  
  11.     Example:
  12.  
  13.     >>> md
  14.     >>> md5fg27dm5p
  15.  
  16.     Between the first letter m and the last letter d, we have the values 5 and 27.
  17.     Thus, we obtain: 5 + 27 = 32
  18.  
  19.     Examples :
  20.     sm
  21.     k36sD4fs65Nd46mKv56v465dmW46d
  22.  
  23.     636
  24.  
  25.  **/
  26.  
  27. int main()
  28. {
  29.     char sep[3];
  30.     scanf("%[^\n]", sep); fgetc(stdin);
  31.     char serie[256];
  32.     scanf("%[^\n]", serie);
  33.     int idxFirst = 0;
  34.     int idxLast = 0;
  35.     for (int i = 0; i < strlen(serie); i++) {
  36.         if (serie[i] == sep[0]) {
  37.             idxFirst = i;
  38.             break;
  39.         }
  40.     }
  41.     for (int i = strlen(serie)-1; i > 0; i--) {
  42.         if (serie[i] == sep[1]) {
  43.             idxLast = i;
  44.             break;
  45.         }
  46.     }
  47.     int sum = 0;
  48.     for (int i = idxFirst; i < idxLast; i++) {
  49.         if (isdigit(serie[i])) {
  50.             sum += serie[i] - '0';
  51.         }
  52.     }
  53.     printf("%d\n", sum);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement