Problem with function

hi everyone...

i have this program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

using namespace std;
int main ()
{
string s1;
string s2;
string s3;
cout<<"Enter first string: ";
getline(cin, s1);

cout<<"Enter second string (to remove): ";
getline(cin, s2);


int indexhere = s1.find(s2); //the starting index (zero-based)
int numberofchars =s2.length(); //the number of characters to take away
if (indexhere!=-1)
s3= s1.erase(indexhere,numberofchars);
cout<<"Substring removed: "<<s3 <<'\n';
else
cout<<"Could'nt find input string in original string.\n";

}



it searches the second string inside the first one and deletes it..that works fine, but here's the problem:

i have to make it as a function with two input string parameters and a return "string" value i.e if i call the function b=delString("abcdef","cde") then b should be b="abf"

something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

char delString(char s1, char s2)

{
    char s3;  
int indexhere = s1.find(s2); 
int numberofchars =s2.length(); 
if (indexhere!=-1)
{
s3= s1.erase(indexhere,numberofchars);
cout<<"Substring removed: "<<s3;
}
else
cout<<"Could'nt find input string in original string.\n";      

return s3;      
}

int main ()
{
char b;
b=delString("abcdef","cde");

return 0;
}


it doesn't work..
first it says " 'find' is not a type"
then it says " 'length' has not been declared", then
" 'erase' has not been declared" and so on...

can anyone please make this function work
Last edited on
char is a single character. So having delString take two chars and return a char isn't useful.

You need to make them strings.



well would you know how to make it work?
I thought it was obvious that the solution was to replace your char type with string.
Topic archived. No new replies allowed.