Cannot return the index number in result

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
58
59
60
61
#include <iostream>
#include<string>
using namespace std;

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

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

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

		char *result = substr(input1, input2);
		cin.ignore();
		if (!*result) 
		{
			//cout << "string not found" << endl;
			//cin.ignore();
			cout << "The string is at : "<<*result+0<< endl;
			cin.ignore();
	
		}
		else
		{
			//cout << "The string is at : "<<*result+0<< endl;
			//cin.ignore();
			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 ((char *)cp);

                cp++;
        }	

        return(NULL); 


}





actually I need to have a output like dis :
1
2
3
4
Input 1 : class
Input 2 : theclass
The string is at : 3


which the class is start at the input 2 theclass , start form [3]
Your function is invalid. For example consider the following statement

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

Why are you returning the first string when the second string is empty? I think you shall return NULL.
Last edited on
@vlad

changed.

if ( !*str2 )
return(NULL);

but the output still same,

STRING NOT FOUND
I did not test the code but hope it will work.

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
#include <iostream>
#include<cstring>

using namespace std;

char * substr(const char* str1,const char* str2)
{
   
   for (  ; *str2; ++str2 )
   {
      const char *p = str1;

      for ( const  char *q = str2; *p && *p == *q; ++p, ++q );

      if ( !*p ) return ( ( char * )str2 );
   }

   return ( NULL );
}

int main()
{
	char* input1=new char[256];
	char* input2=new char[256];
	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 - input2 << endl;
	
	}
	else
	{
		cout << "string not found" << endl;
	}
}
It's work ! Thanks you !

By the way , can explain why the for loop

the first condition u just blank it?
Because the first part is normally the initialisation of the counting variable.
In this case however, a pointer to str2 is the counter and it is already defined, so there's no need to define another counter variable.

Hope that makes sense.

All the best,
NwN
okay thanks u

@nwn , thanks ya
Topic archived. No new replies allowed.