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.
#include <cstring>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int myStrLen(constchar[]); // return length of null-terminated string stored in char array
void myStrCpy(char[], constchar[]); // copy the contents of the 2nd string into the 1st
int myStrCmp(constchar[], constchar[]); // 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(constchar 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[], constchar 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(constchar string1[], constchar string2[])
{
for (int i = 0;; i++)
{
if (string1[i] == string2[i])
return 0;
elsereturn 1;
}
} // end myStrCmp
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.
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.