Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdbool.h>
- #include <assert.h>
- char *getSpecialString(
- char *buffer,
- int bufferLength,
- const char *str,
- const char *str2);
- int main()
- {
- int bufLen = 2;
- char buf[bufLen];
- char *result = getSpecialString(
- buf,
- bufLen,
- "Hhel12323Llmo th34-ere!!",
- "hll e!");
- assert(0 == strcmp(result, "h"));
- return EXIT_SUCCESS;
- }
- char *getSpecialString(
- char *buffer,
- int bufferLength,
- const char *str,
- const char *str2)
- {
- int numberOfLetters = 'z' - 'a' + 1;
- bool letterIsPresent[numberOfLetters];
- for (int i = 0; i < numberOfLetters; i++)
- {
- letterIsPresent[i] = false;
- }
- int str2Len = strlen(str2);
- for (int i = 0; i < str2Len; i++)
- {
- if (islower(str2[i]))
- {
- int n = str2[i] - 'a';
- letterIsPresent[n] = true;
- if (n >= 1)
- {
- letterIsPresent[n - 1] = true;
- }
- if (n < numberOfLetters - 1)
- {
- letterIsPresent[n + 1] = true;
- }
- }
- }
- int index = 0;
- int strLen = strlen(str);
- for (int i = 0; index < bufferLength - 1 && i < strLen; i++)
- {
- if (islower(str[i]) && letterIsPresent[str[i] - 'a'])
- {
- buffer[index] = str[i];
- index++;
- }
- }
- buffer[index] = '\0';
- return buffer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement