Petkovsky
- 61
- 0
Homework Statement
Ok, i need to make a program that will search for a substring in a given string, and replace it with another one.
The Attempt at a Solution
Code:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;int fsubstitute(char *ulazni, char *prvi, char *drugi);
int main()
{
char initial[255];
char find[255];
char replace[255];
gets(initial);
gets(find);
gets(replace);
fsubstitute(initial,find,replace);
}
int fsubstitute(char *initial, char *find, char *replace)
{
char *temp1;
int i,j,k,c,l,n;
i = j = k = c = l = n = 0;
n = strlen(find);
l = strlen(initial);
char temp2[200];
do{
if(strstr(initial,find)!= NULL)
{
temp1 = strstr(initial,find)+ n;
for(i=0;i<l-strlen(temp1)-n;i++)
temp2[i] = *(initial+i);
temp2[i] = '\0';
initial = temp2;
initial = strcat(initial,replace);
initial = strcat(initial,temp1);
}
}while(strstr(initial,find)!= NULL);
cout << initial << endl;
return 0;
};
I used cout since it is easier to write, and that's why i included a c++ library/
Can you tell me what's wrong with my code?