Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdarg.h>
- #include <vector>
- #include <string>
- #define MAXMSG 512
- #define COLOR_GREEN "\x1B[32m"
- #define COLOR_RESET "\033[0m"
- typedef std::vector<std::string> MyArray;
- char* change_color(char* str, ...)
- {
- static char output[MAXMSG];
- char string[MAXMSG];
- va_list args;
- va_start(args, str);
- vsprintf(string, str, args);
- va_end(args);
- sprintf(output, "%s%s%s", COLOR_GREEN, string, COLOR_RESET);
- return output;
- }
- void search_in_array(MyArray& pArray, const char *key)
- {
- int i, n = pArray.size();
- bool bFound = false;
- for (i = 0; i < n; i++)
- {
- if (strstr(pArray[i].c_str(), key) != NULL)
- {
- printf("String found: %s\n", pArray[i].c_str());
- char *colored = change_color("%s", pArray[i].c_str());
- printf("Changed color: %s\n", colored);
- bFound = true;
- }
- }
- if (!bFound) printf("Can not find string with given key: %s\n", key);
- }
- int main(int argc, char *argv[])
- {
- MyArray sArray;
- sArray.clear();
- if (argc < 2)
- {
- printf("Usage: %s [name]\n", argv[0]);
- printf("Example: %s lectus\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- sArray.push_back("lorem ipsum dolor sit amet");
- sArray.push_back("consectetur adipiscing elit");
- sArray.push_back("donec a diam lectus");
- sArray.push_back("sed sit amet ipsum mauris");
- search_in_array(sArray, argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement