How do you test a compare function with a parameter that is a blank string?

Hi,

I'm stuck on the last step of my lab. It is asking us to
Modify your code by adding your own tests to see if your functions work right. Include at least 6 separate tests, of your choosing.

For example, test the compare function with the first parameter as a blank string -- then with the 2nd as a blank -- then both. Test compare with the first string shorter than the second -- then the other way around. Test your copy function with long strings.


I am struggling with how to use the compare function with a parameter as a blank string. I tried leaving the first parameter blank but doing ("",text) but I don't think that is the correct way of doing this.

If anyone could give me any help I'd greatly appreciate it.

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
83
84
85
86
87
88
89
90
#include <cstring>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int myStrLen(const char[]); // return length of null-terminated string stored in char array
void myStrCpy(char[], const char[]); // copy the contents of the 2nd string into the 1st
int myStrCmp(const char[], const char[]); // return 0 if the 2 strings are equal, else return 1

int main()
{
	
		// read any string
		char text[128]; // storage for 128 characters
		cout << "Enter a word of phrase. Press ENTER when you're finished." << endl;
		cin.getline(text, 128);

		// create another string for comparing
		char abc[] = "ABC";

		// print the string and compare it
		cout << "String: " << text << endl;
		cout << "Length: " << myStrLen(text) << endl;
	
		if (myStrCmp(text, abc) == 0)
			cout << "Your string equals ABC" << endl;
		else
			cout << "Your string does not equal ABC" << endl;

		// change the string, print it, and compare it
		myStrCpy(text, "ABC");
		cout << "String: " << text << endl;
		cout << "Length: " << myStrLen(text) << endl;
		if (myStrCmp(text, abc) == 0)
			cout << "The changed string equals ABC" << endl;
		else
			cout << "The changed string does not equal ABC" << endl;
	
} // end main()

int myStrLen(const char c[])
{
	int charCounter = 0; // records # of chars in array

	// count the chars
	for (int i = 0;; i++)
	{
		if (c[i] == '\0')
			break;
		else
			charCounter++;
	}

	return charCounter;
} // end myStrLen()
	
void myStrCpy(char newArray[], const char orgArray[])
{
	// erasing contents of original array
	for (int z = 0;; z++)
	{
		if (newArray[z] == '\0')
			break;
		else
			newArray[z] = 0;
	} 

	// copying orgArray into newArray
	for (int a = 0;; a++)
	{
		if (orgArray[a] == '\0')
			break;
		else
			newArray[a] = orgArray[a];
	}	 
	cout << endl;
} // end myStrCopy()

int myStrCmp(const char string1[], const char string2[])
{
	for (int i = 0;; i++)
	{
		if (string1[i] == string2[i])
			return 0;
		else
			return 1;
	} 
} // end myStrCmp
Last edited on
I think I sort of figured it out. Can anyone confirm that this is correct? However, I'm not sure how to compare both parameters as blank strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	// test compare function with the second parameter as a blank string
	cout << "string: " << text << endl;
	cout << "length: " << myStrLen(text) << endl;
	if (myStrCmp(text, "") == 0)
		cout << "your string is blank" << endl << endl;
	else 
		cout << "your string is not blank" << endl << endl;

	// test compare function with both parameters as blank strings
	cout << "string: " << text << endl;
	cout << "length: " << myStrLen(text) << endl;
	if (myStrCmp("", "") == 0)
		cout << "your string is blank" << endl << endl;
	else 
		cout << "your string is not blank" << endl << endl;
Your function myStrCmp is invalid. It compares only first characters of arrays. Also it is not clear why your function returns an integer value instead of a boolean value.

I would rewrite it the following way

1
2
3
4
5
bool myStrCmp( const char s1[], const char s2[] )
{
	while ( *s1 && *s1 == *s2 ) ++s1, ++s2;
	return (  *s1 == *s2 );
}


The int return type has meaning in case when the function returns three values instead of two. For example 1 means that the first char array is greater than the second, 0 means that the arrays are equal, and -1 means that the first array is less than the second array.
Topic archived. No new replies allowed.