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;
}

Recommended Answers

All 6 Replies

I got my code to work for one character but anything more I get wrong output. It seems to pulling other characters. Heres the code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int length(const char str[])
{
	int i=0;
	while(str[i] != '\0')
	{
		i++;
	}
	return i;
}

bool replace(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(str);
    for(int i=0; i < len; i++)
	{
        int k = 0;
        if(str[i] == find[k])
		{
            cout << "str: " << str[i] << " " << "find: " << find[i] << " " << "replace: " << replace[i] << endl; 
			str[i] = replace[k];
        }
        k++;
    }
    return true;
}
		

		

int main ()
{
	char array[]="there";
	char array2[]="re";
	char array3[]="ir";
	replace(array, array2, array3, true);
	cout << "array: " << array <<endl;
	return 0;
}

The output:

str: r find: t replace: r
array: theie

Well, there is nothing for it. Now you must implement strstr() RTL function. What's an avalanche...
Why "ERROR: The find and replace strings are of different length"? Why so truncated replace? It looks like a castrated tomcat...
What's is_partial_match role?

I can't use any sort of string functions. As far as the "ERROR", the find and replace strings must be equal in length. That's what is outlined for the specs. is_partial_match has no role and is just a carry over from something else.

I never told about USING of strstr function. You must IMPLEMENT this function. You have implemented strlen (under length alias). The strstr(s,p) searches substring p occurence in a string s - it's exactly what you need to implement your replace.
Now your internal loop does nothing useful. It's impossible to correct it: you must replace it from the scratch.

I've been having a real problem trying to implement this. I really don't know where to start. I've been trying this for hours. Generally I have some sort of an idea, but this one has me licked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.