I'm solving a problem with find and replace a substring in a string. I've solved it using Python with a couple of lines, in C++ using std library and now solved it with C by using allocation on the stack. I want to get genuine feedback on the code I wrote and perhaps make it simpler.
The constraints:
- do not use standard library, write logic by hand. (I've included stdio.h primarily for debugging. Final version of the algorithm could be a function like: void find_and_replace(char* source, char* find, char* replace); )
Requirements:
- Replace should replace all matches
- Match and replace same length string
- Replace longer match with shorter replacement
- Replace shorter match with longer replacement
#include <stdio.h>
int main()
{
enum SIZE
{
ARRAY_MAX = 50
};
char original[ARRAY_MAX] = "one two three two\0";
char find[ARRAY_MAX] = "two\0";
char replace[ARRAY_MAX] = "22222\0";
printf("original: %s\n", original);
printf("find: %s\n", find);
printf("replace: %s\n", replace);
char* current = original;
while (*current != '\0')
{
if (*current == find[0])
{
char* match_iter = current;
char* find_iter = find;
int match = 0;
while(*match_iter!='\0' && *find_iter!='\0')
{
if (*match_iter == *find_iter)
{
match = 1;
}
else
{
match = 0;
break;
}
match_iter++;
find_iter++;
}
if (match)
{
printf("the whole word matched\n");
find_iter = find;
char* replace_iter = replace;
while(*find_iter != '\0' &&
*current != '\0' &&
*replace_iter != '\0')
{
*current = *replace_iter;
++find_iter;
++replace_iter;
++current;
}
if (*find_iter != '\0' &&
*replace_iter == '\0')
{
printf("match is longer than replace\n");
char* move_left = current;
while(*find_iter != '\0' &&
*move_left != '\0')
{
++find_iter;
++move_left;
}
char* temp_current = current;
while(*move_left != '\0' &&
*temp_current != '\0')
{
*temp_current = *move_left;
++temp_current;
++move_left;
}
*temp_current = '\0';
}
else if (*find_iter == '\0' &&
*replace_iter != '\0')
{
printf("replace is longer than match\n");
char* move_right = current;
char temp[ARRAY_MAX];
char* temp_iter = temp;
while(*replace_iter != '\0')
{
*temp_iter = *current;
*current = *replace_iter;
++current;
++temp_iter;
++replace_iter;
}
char* current_to_end = current;
while(*current_to_end != '\0')
{
*temp_iter = *current_to_end;
++temp_iter;
++current_to_end;
}
*temp_iter = '\0';
temp_iter = temp;
char* temp_current = current;
while(*temp_iter != '\0')
{
*temp_current = *temp_iter;
++temp_current;
++temp_iter;
}
*temp_current = '\0';
}
else if (*find_iter == '\0' &&
*replace_iter == '\0')
{
printf("replace and match are same length\n");
}
}
else
{
printf("only a fraction matched\n");
}
}
++current;
}
printf("The sentence after replacement: %s\n", original);
}
#include <stdio.h>
. Is it meant to be something else? \$\endgroup\$<stdio.h>
is for...well i/o. Code sold1er is trying to show an algorithm for find/replace in string...not re-implementing standard input/output. \$\endgroup\$C
. Without the STL (ie the algorithms in thestd::
) and in this sort of problem domain anyC++
solution is going to be awfully close to theC
solution. We could "introduce" someC++
egconstexpr
for the enum, but it would be a bit arbitrary..? Is the aim to make the code "better and shorter" or "to use C++"? \$\endgroup\$std
has special meaning in C++, it's the namespace for the standard library, and that's what the OP meant I believe. (so<stdio.h>
is not part of that, because it's a C header for input output and not part of namespacestd::
). I presume you know all this? \$\endgroup\$