diff --git a/substringSearch/substringSearch.sln b/substringSearch/substringSearch.sln new file mode 100644 index 0000000..4fde923 --- /dev/null +++ b/substringSearch/substringSearch.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "substringSearch", "substringSearch\substringSearch.vcxproj", "{991473C0-010E-492B-91D1-3EFBDEB6FAB3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x64.ActiveCfg = Debug|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x64.Build.0 = Debug|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x86.ActiveCfg = Debug|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Debug|x86.Build.0 = Debug|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x64.ActiveCfg = Release|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x64.Build.0 = Release|x64 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x86.ActiveCfg = Release|Win32 + {991473C0-010E-492B-91D1-3EFBDEB6FAB3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7A1CB94E-03B1-4922-AC28-FD130F1083F6} + EndGlobalSection +EndGlobal diff --git a/substringSearch/substringSearch/KMP.c b/substringSearch/substringSearch/KMP.c new file mode 100644 index 0000000..eaa1092 --- /dev/null +++ b/substringSearch/substringSearch/KMP.c @@ -0,0 +1,47 @@ +#include "KMP.h" +#include +#include +#include + +int* prefixFunction(const char* string) { + int* prefixArray = calloc(strlen(string), sizeof(int)); + if (prefixArray == NULL) { + return NULL; + } + prefixArray[0] = 0; + size_t sizeString = strlen(string); + for (size_t i = 1, j = 0; i < sizeString; ++i) { + while (j > 0 && string[j] != string[i]) { + j = prefixArray[j - 1]; + } + if (string[i] == string[j]) { + ++j; + } + prefixArray[i] = j; + } + return prefixArray; +} + +int findIndexSubstring(const char* string, const char* substring, int* errorCode) { + size_t sizeString = strlen(string); + size_t sizeSubstring = strlen(substring); + int result = -1; + if (sizeString == 0 || sizeSubstring == 0 || sizeString < sizeSubstring) { + return -1; + } + int* prefixArray = prefixFunction(substring); + for (int i = 0, j = 0; i < sizeString; ++i) { + while (j > 0 && substring[j] != string[i]) { + j = prefixArray[j - 1]; + } + if (substring[j] == string[i]) { + ++j; + } + if (j == sizeSubstring) { + free(prefixArray); + return i - j + 1; + } + } + free(prefixArray); + return -1; +} diff --git a/substringSearch/substringSearch/KMP.h b/substringSearch/substringSearch/KMP.h new file mode 100644 index 0000000..c9acd43 --- /dev/null +++ b/substringSearch/substringSearch/KMP.h @@ -0,0 +1,4 @@ +#pragma once + +//the KMP algorithm for finding a substring in a string +int findIndexSubstring(const char* string, const char* substring, int* errorCode); \ No newline at end of file diff --git a/substringSearch/substringSearch/main.c b/substringSearch/substringSearch/main.c new file mode 100644 index 0000000..51ca3ed --- /dev/null +++ b/substringSearch/substringSearch/main.c @@ -0,0 +1,148 @@ +#include "test.h" +#include "KMP.h" +#include +#include +#include +#include + +char* getLine(int* lengthString, int* errorCode) { + *lengthString = 0; + int index = 1; + char* string = (char*)malloc(sizeof(char)); + if (string == NULL) { + *errorCode = -1; + return NULL; + } + char symbol = getchar(); + while (symbol != '\n') { + string[*lengthString] = symbol; + ++(*lengthString); + if (*lengthString >= index) { + index *= 2; + char* copy = (char*)realloc(string, index * sizeof(char)); + if (copy == NULL) { + *errorCode = -1; + free(string); + return NULL; + } + string = copy; + } + symbol = getchar(); + } + if (string == NULL) { + *errorCode = -1; + return NULL; + } + string[*lengthString] = '\0'; + + return string; +} + +int workWithFile(const char* nameFile, int* errorCode, int sizeSubstring, const char* substring) { + FILE* file = NULL; + *errorCode = fopen_s(&file, nameFile, "r"); + if (*errorCode != 0 || file == NULL) { + *errorCode = -1; + return -1; + } + + char buffer[400] = { '\0' }; + char copyText[200] = { '\0' }; + int steps = 1; + size_t sizeBuffer = 0; + size_t sizeCopyText = 0; + int index = 0; + int indexToReturn = 0; + while (fgets(copyText, 200, file) != NULL) { + switch (steps) + { + case 1: + index = findIndexSubstring(copyText, substring, errorCode); + strcpy(buffer, copyText); + steps = 2; + break; + case 2: + sizeBuffer = strlen(buffer); + sizeCopyText = strlen(copyText); + for (int i = sizeBuffer; i < sizeBuffer + sizeCopyText; ++i) { + buffer[i] = copyText[i - 199]; + } + steps = 3; + index = findIndexSubstring(buffer, substring, errorCode); + break; + case 3: + indexToReturn += 199; + sizeBuffer = strlen(buffer); + if (sizeBuffer >= 200) { + int difference = sizeBuffer - 199; + for (int i = sizeBuffer - 199; i < sizeBuffer; ++i) { + buffer[i - difference] = buffer[i]; + } + } + sizeCopyText = strlen(copyText); + for (int i = 0; i < sizeCopyText; ++i) { + buffer[i + 199] = copyText[i]; + } + index = findIndexSubstring(buffer, substring, errorCode); + break; + } + if (index != -1) { + fclose(file); + return index + indexToReturn; + } + if (*errorCode != 0) { + fclose(file); + return -1; + } + memset(copyText, 0, 200); + } + fclose(file); +} + +int main() { + if (test()) { + printf("Test correct!\n"); + } else { + printf("Test not correct!\n"); + } + printf("Input file name with extension\n"); + int errorCode = 0; + int sizeFile = 0; + const char* nameFile = getLine(&sizeFile, &errorCode); + if (errorCode != 0) { + printf("Error\n"); + return -1; + } + printf("Input substring. No more than 99 symbols\n"); + int sizeSubstring = 0; + char* substring = getLine(&sizeSubstring, &errorCode); + if (errorCode != 0 || substring == NULL) { + printf("Error\n"); + free(nameFile); + return -1; + } + while (sizeSubstring > 100) { + printf("No more than 99 symbols"); + free(substring); + substring = getLine(&sizeSubstring, &errorCode); + if (errorCode != 0 || substring == NULL) { + printf("Error\n"); + free(nameFile); + return -1; + } + } + int number = workWithFile(nameFile, &errorCode, sizeSubstring, substring); + if (errorCode != 0) { + printf("Error\n"); + free(nameFile); + free(substring); + return -1; + } + free(nameFile); + free(substring); + if (number != -1) { + printf("%d\n", number); + } else { + printf("No occurrences found\n"); + } +} \ No newline at end of file diff --git a/substringSearch/substringSearch/substringSearch.vcxproj b/substringSearch/substringSearch/substringSearch.vcxproj new file mode 100644 index 0000000..fc93fba --- /dev/null +++ b/substringSearch/substringSearch/substringSearch.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {991473c0-010e-492b-91d1-3efbdeb6fab3} + substringSearch + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/substringSearch/substringSearch/substringSearch.vcxproj.filters b/substringSearch/substringSearch/substringSearch.vcxproj.filters new file mode 100644 index 0000000..49d7380 --- /dev/null +++ b/substringSearch/substringSearch/substringSearch.vcxproj.filters @@ -0,0 +1,41 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Исходные файлы + + + + + Исходные файлы + + + Исходные файлы + + + \ No newline at end of file diff --git a/substringSearch/substringSearch/test.c b/substringSearch/substringSearch/test.c new file mode 100644 index 0000000..45aa14d --- /dev/null +++ b/substringSearch/substringSearch/test.c @@ -0,0 +1,15 @@ +#include "test.h" +#include "KMP.h" + +bool test() { + int errorCode = 0; + bool itIsOK = findIndexSubstring("error", "ror", &errorCode) == 2; + if (errorCode != 0 || !itIsOK) { + return false; + } + itIsOK = findIndexSubstring("error", "a", &errorCode) == -1; + if (errorCode != 0 || !itIsOK) { + return false; + } + return findIndexSubstring("error", "rortutuut", &errorCode) == -1 && errorCode == 0; +} \ No newline at end of file diff --git a/substringSearch/substringSearch/test.h b/substringSearch/substringSearch/test.h new file mode 100644 index 0000000..4793ad5 --- /dev/null +++ b/substringSearch/substringSearch/test.h @@ -0,0 +1,5 @@ +#pragma once +#include + +//Tests for find position substring +bool test(void); \ No newline at end of file diff --git a/substringSearch/substringSearch/test.txt b/substringSearch/substringSearch/test.txt new file mode 100644 index 0000000..70bff31 --- /dev/null +++ b/substringSearch/substringSearch/test.txt @@ -0,0 +1 @@ +I go home. ereadsbdfbgdsfdiofjsofijw9uhgwglkfdfijsfierrrrrrrrrrrrrrrrrrrrrrrrrrrer ere rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrre erererer erettttttttttttttttttttttttttttttttt ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt erererer eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeap \ No newline at end of file