I need to design a function to find within a string a match to what is being searched and then replace that string with another. For example I need to search the string "baseball" with the string "base" and replace it with "basket". The code I have so far does not replace. This is what I got so far:
#include <iostream>
using namespace std;
int length(const char str[])
{
int i=0;
while(str[i] != '\0')
{
i++;
}
return i;
}
bool str_replace_first_iter(char str[], const char find[], const char replace[], bool is_partial_match)
{
if(length(find) != length(replace))
{
cout << "ERROR: The find and replace strings are of different length." << endl;
cout << "Please change the find and replace strings to the same length." << endl;
return false;
}
int len = length_iter(str);
for(int i=0; i < len; i++)
{
if(str[i] == find[i])
{
cout << "str: " << str[i] << " " << "find: " << find[i] << " " << "replace: " << replace[i] << endl;
str[i] = replace[i];
}
}
return true;
}
int main ()
{
char array[]="hella";
char array2[]="a";
char array3[]="o";
str_replace_first_iter(array, array2, array3, true);
cout << "array: " << array <<endl;
return 0;
}