Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <assert.h>
  7.  
  8. char *getSpecialString(
  9.     char *buffer,
  10.     int bufferLength,
  11.     const char *str,
  12.     const char *str2);
  13.  
  14. int main()
  15. {
  16.     int bufLen = 2;
  17.     char buf[bufLen];
  18.     char *result = getSpecialString(
  19.         buf,
  20.         bufLen,
  21.         "Hhel12323Llmo th34-ere!!",
  22.         "hll e!");
  23.     assert(0 == strcmp(result, "h"));
  24.     return EXIT_SUCCESS;
  25. }
  26.  
  27. char *getSpecialString(
  28.     char *buffer,
  29.     int bufferLength,
  30.     const char *str,
  31.     const char *str2)
  32. {
  33.     int numberOfLetters = 'z' - 'a' + 1;
  34.     bool letterIsPresent[numberOfLetters];
  35.     for (int i = 0; i < numberOfLetters; i++)
  36.     {
  37.         letterIsPresent[i] = false;
  38.     }
  39.     int str2Len = strlen(str2);
  40.     for (int i = 0; i < str2Len; i++)
  41.     {
  42.         if (islower(str2[i]))
  43.         {
  44.             int n = str2[i] - 'a';
  45.             letterIsPresent[n] = true;
  46.             if (n >= 1)
  47.             {
  48.                 letterIsPresent[n - 1] = true;
  49.             }
  50.             if (n < numberOfLetters - 1)
  51.             {
  52.                 letterIsPresent[n + 1] = true;
  53.             }
  54.         }
  55.     }
  56.     int index = 0;
  57.     int strLen = strlen(str);
  58.     for (int i = 0; index < bufferLength - 1 && i < strLen; i++)
  59.     {
  60.         if (islower(str[i]) && letterIsPresent[str[i] - 'a'])
  61.         {
  62.             buffer[index] = str[i];
  63.             index++;
  64.         }
  65.     }
  66.     buffer[index] = '\0';
  67.  
  68.     return buffer;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement