Return s1 in the address of s2

Pages: 12
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include<string>
using namespace std;

char * strstr(const char *, const char *);

void main()
{
	char input1[256] , input2[256];

	cout<<"Input 1 : ";
	cin.getline(input1,256);

	cout<<"Input 2 : ";
	cin.getline(input2,256);

	strstr(input1,input2);

	
}

char * strstr(
        const char * str1,
        const char * str2
        )
{
        char *cp = (char *) str1;
        char *s1, *s2;

        if ( !*str2 )
            return((char *)str1);

        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;

                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;

                if (!*s2)
                        return(cp);

                cp++;
        }

        return(NULL);


}



Can someone help me why i get error ? and help me to fix it?
trying many times already..
Last edited on
1) Use code tags.
2) What errors are you having ? Post them and we can help you.
1
2
3
4
5
6
7
1>c:\users\wendy chirs\desktop\inti\oop\jye\jye\jye\testingcall.cpp(5): error C2556: 'char *strstr(const char *,const char *)' : overloaded function differs only by return type from 'const char *strstr(const char *,const char *)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(196) : see declaration of 'strstr'
1>c:\users\wendy chirs\desktop\inti\oop\jye\jye\jye\testingcall.cpp(5): error C2373: 'strstr' : redefinition; different type modifiers
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(196) : see declaration of 'strstr'
1>c:\users\wendy chirs\desktop\inti\oop\jye\jye\jye\testingcall.cpp(26): error C2556: 'char *strstr(const char *,const char *)' : overloaded function differs only by return type from 'const char *strstr(const char *,const char *)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(196) : see declaration of 'strstr'
1>c:\users\wendy chirs\desktop\inti\oop\jye\jye\jye\testingcall.cpp(26): error C2491: 'strstr' : definition of dllimport function not allowed
Can you try changing you function's name ?

Edit: Don't use void main(). int main() is the right way to go.
Last edited on
Dash .
it's work after i changing the function name.
can i ask what problem to cause that?
Beside this .
i want to add-on a things that

Input 1 : class
Input 2 : roomclass

The class in the input 2 occur in the address of 4.


The 4 address how should i write it to show it?

Thanks by the way . u help me solve my toughest part
It's because there is a function named strstr that returns a const char*, and as the error says, you can't overload functions based on the return type alone.
There was no need to rename your function. You could access it by specifying global namespace as, for example:

::strstr(input1,input2);

Last edited on
vlad .
thanks .

okay. thanks anyways.
so the when i match the input .

then how should i print out the address ?

Last edited on
vlad, I that doesn't solve the problem, as std::strstr() is defined in the std namespace AND in the global namespace as well: so ::strstr() would still clash with the standard strstr().
I don't know how to print out the address

1
2
3
4
Input 1 : Class
Input 2 : Theclass

The address of s1 in s2 is : 3


how to print out the 3?
i think my code should not be problem already?

@R0mai
vlad, I that doesn't solve the problem, as std::strstr() is defined in the std namespace AND in the global namespace as well: so ::strstr() would still clash with the standard strstr().



It is implementation defined whether C standard function are defined in the global namespace or not. Moreover in the original code the header <cstring> is not included. So it can be that in the header <string> only using declarations are used which do not include strstr.


i asking for so many times..

Any senior can help me please?
You're right vlad. But in VS, (what OP is using) <cstring> is included (probably from <string>).

@Lim Boon Jye : You can do additive arithmetic with pointers, so subtract input1 from the result of strstr(). This should give you 3. (Of course, check for NULL before).
Last edited on
u Mean i add a

1
2
3
char *total;

total = input1 - 1;


i put this code inside my strstr() function isnt?

and put before return(null)?;

The result that i get from strstr() ;

where can i put my formula in? and check for NULL before is? sorry for that..
I mean like this in your main :

1
2
3
4
5
6
char *result = strstr(input1, input2);
if ( !result ) {
    std::cout << result - input1 << std::endl;
} else {
    std::cout << "string not found" << std::endl;
}


Edit : edited if
Last edited on
after i put it.

It say the string not found .

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include<string>
using namespace std;

char * substr(const char *, const char *);

void main()
{
	char input1[256] , input2[256];
	char *back;
	cout<<"Input 1 : ";
	cin.getline(input1,256);

	cout<<"Input 2 : ";
	cin.getline(input2,256);

	char *result = substr(input1, input2);
	if ( result ) 
	{
		cout << "The string is at : "<<result - input1 << endl;
	}
	else
	{
		cout << "string not found" << endl;
	}
	cin.ignore();
}

char * substr(
        const char * str1,
        const char * str2
        )
{
        char *cp = (char *) str1;
        char *s1, *s2;

        if ( !*str2 )
            return((char *)str1);

        while (*cp)
        {
                s1 = cp;
                s2 = (char *) str2;

                while ( *s1 && *s2 && !(*s1-*s2) )
                        s1++, s2++;

                if (!*s2)
                        return(cp);

                cp++;
        }

        return(NULL);


}
I'm sorry, you're right. The condition shoudl be if ( !result ).
Last edited on
this time my output is :

1
2
3
4
Input 1: yes
Input 2: ohyes

The string is at : -4650544


why de?
actually i wan my output is like dis :

1
2
3
Input 1: yes
Input 2: ohyes
The String s1 is start at s2 at : 2


Mean from [0] , [1] , then [2]
Pages: 12