methods overloading

Evening everyone, need your help with the following task:
I have to write overloading methods that will do ( strcpy, strlen, strcat, strstr, strchr, strcmp) for char arrays. ( Next lesson our teacher will introduce the string library and will get over char data type.)

I have to mention that the code should be done in a specific way , and it has to include constructors, set and get and all the basic info.

I've done the strcpy overloading method, however I need a hint or reference how to continue with other functions.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

class sir 
{
	char array[256];
	char c;
	
	public:
		
		//implicit constructor;
		sir (char array[0]=0){strcpy(this->array,array), this->c=0;}
		sir (char c){this->c=c, this->array[0]=0;}
		
		//overloading constructor;
		sir (char array[], char c){this->c=c, strcpy(this->array,array);}
		
		//copy constructor;
		sir (const sir&OBJ)
		{
			this->c=OBJ.c;
			strcpy(this->array,OBJ.array);
		}

//SET&GET CHAR ARRAY		
inline void setA(){cout<<"Enter array "; cin>>this->array;}	
inline void setAA(char array[]){cout<<"Enter array "; strcpy(this->array,array);}
inline const char* getA()const {return this->array;}

//SET&GET CHAR
inline void setC(){cout<<"Enter char "; cin>>this->c;}	
inline void setCA(char c){cout<<"Enter char "; this->c=c;}
inline const char getC()const {return this->c;}

friend ostream& operator<<(ostream&OUT, sir&X);
friend istream& operator>>(istream&IN, sir&X);


 void operator=(char x)
 {
 this->c=x;
 }
 
 
 void operator=(char array[])
 {
 strcpy(this->array,array);
 cout<<this->array<<endl;
 }
 
 
};


ostream& operator<<(ostream&OUT, sir&X)
{
	OUT<<X.array<<endl;
	
	return OUT;
}

istream& operator>>(istream&IN, sir&X)
{
	cout<<"Enter array "<<endl;
	IN>>X.array;
	
	return IN;
}


int main()
{
	
 sir A("aloha"),B('T'), C("EUROPE");

cout<<A;
cout<<B.getC()<<endl;

A=C; //

cout<<A<<C<<endl;

return 0;
}
Last edited on
its all about finding the zero for C-strings.
you iterate from the beginning until you find a character that is literally the value 0 or if you want to type more letters, '\0'.
that tells you the length.
it tells you where to start copying into it for concatenation.

for strstr, you unfortunately need to pattern match the darn thing.
something like
for every letter in the target,
does that letter match the first letter of what you are looking for?
if it does, you can get snarky and use memcmp on the block of bytes or you can just iterate and check matches in a loop. If you found it, return the address of the first letter, if not, return 0 (null pointer).
strcmp is just a wrapper for memcmp or you can loop it here. find the lengths of both, if they are equal, then check the data, if not equal, early free exit (no match).

maybe that will get you started? None of these should be any harder than what you already have done here.
extra credit: you can cheat since its your own class and keep the size of the arrays in hand without seeking the zero all the time. There are 2 ways to do this: you can have an extra character and use the first location [0] as the length (0-255 chars) or you can have a new class member. If you use the first location, you have to remember to return &string[1] instead of just string each time you need to be compatible with C-strings. C++ allows negative indexing, so you can say mystring[-1] to recover it out in the wild. This is sort of going down a weird rabbit hole, but its been done this way (I think this idea is called a pascal string, from the language pascal which I think is dead now).
Last edited on
jonnin, many thanks, great explanation, I will try to do my best
(
I think this idea is called a pascal string, from the language pascal which I think is dead now).


Yes, pascal stores the length of the string first before the text. Fortunately, pascal is still alive and very much kicking.

There is a MS hybrid type BSTR which combines both - holds the length of the string, the data and the terminating null. See https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
that will do ( strcpy, strlen, strcat, strstr, strchr, strcmp) for char arrays.
...
I've done the strcpy overloading method


I don't see any code that does a strcpy()? Are you required to provide your own versions of these c-string functions - or do you want need to wrap the existing standard functions into class methods?

If you are to provide your own versions of the c-string functions, then for simple versions consider (with a trailing 1 to avoid duplicate of the standard ones):

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
void strcpy1(char* s1, const char* s2)
{
	while (*s1++ = *s2++);
}

size_t strlen1(const char* s)
{
	const char* sc {s};

	for (; *sc; ++sc);

	return sc - s;
}

void strcat1(char* s1, const char* s2)
{
	char* s {s1};

	for (; *s; ++s);
	for (; (*s = *s2); ++s, ++s2);
}

const char* strchr1(const char* s, char c)
{
	for (; *s != c; ++s)
		if (!*s)
			return nullptr;

	return s;
}

const char* strstr1(const char* s1, const char* s2)
{
	if (!*s2)
		return s1;

	for (; (s1 = strchr1(s1, *s2)); ++s1) {
		const char* sc1 {s1}, *sc2 {s2};

		while (true)
			if (!*++sc2)
				return s1;

		if (*++sc1 != *sc2)
			break;
	}

	return nullptr;
}

int strcmp1(const char* s1, const char* s2)
{
	for (; *s1 == *s2; ++s1, ++s2)
		if (!*s1)
			return 0;

	return *(unsigned char*)s1 < *(unsigned char*)s2 ? -1 : 1;
}


@seeplus,
I have to provide my own versions of cstring functions, mentioned above.

as follows, the strcpy according to my point of view could be :

void operator=(char array[])
{
strcpy(this->array,array);
cout<<this->array<<endl;
}

Thank you for so much help and in depth code!
Last edited on
Topic archived. No new replies allowed.