totally confused on what to do. Can someone start me off on the right place?
I need to include this function in my code:
int comp201_strcmp(char str1[], char str2[]);
This function will take in two c-style strings, meaning that they are an array of characters, each terminated
with the null ('\0') character. The return value is based on the following rules:
-1 the first character that does not match has a lower value in str1 than in str2 (e.g. str1 is shorter)
0 the contents of both strings are equal
1 the first character that does not match has a greater value in str1 than in str2 (e.g. str1 is longer)
Thank you!!
1 2 3 4 5 6 7 8 9
#include <iostream>
usingnamespace std;
int comp201_strcmp(char str1[], char str2[]);
int main()
{
}
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2
I suspect the OP is being asked to write his own version of strcmp so that he understands what strcmp does.
gmac wrote:
I just dont know how to use the char str1[] and char str2 [].
Think about the algorithm of how to compare two strings. You want to iterate through the two strings comparing each respective character.
Don't forget about boundary cases. Either or both strings could be empty.
One string could be longer that the other. i.e. One string contains "ABC", while the other string contains "ABCD".
#include <iostream>
usingnamespace std;
// Preconditions: str1 is a c-style string
// str2 is a c-style string
// Returns: -1 if the first character that does not match has a lower value in str1 than in str2 (e.g. str1 is shorter)
// 0 if the contents of both strings are equal
// 1 if the first character that does not match has a greater value in str1 than in str2 (e.g. str1 is longer)
int comp201_strcmp(char str1[], char str2[]);
int main()
{
char str1[] = "hello";
char str2[] = "hello friend";
char str3[] = "Hello";
char str4[] = "";
cout << "Same: " << comp201_strcmp(str1, str1) << " = 0" << endl;
cout << "1 shorter: " << comp201_strcmp(str1, str2) << " = -1" << endl;
cout << "2 shorter: " << comp201_strcmp(str2, str1) << " = 1" << endl;
cout << "1 greater: " << comp201_strcmp(str1, str3) << " = 1" << endl;
cout << "2 greater: " << comp201_strcmp(str3, str1) << " = -1" << endl;
cout << "1 blank: " << comp201_strcmp(str4, str1) << " = -1" << endl;
cout << "2 blank: " << comp201_strcmp(str1, str4) << " = 1" << endl;
cout << "Both blank: " << comp201_strcmp(str4, str4) << " = 0" << endl;
return 0;
}
int comp201_strcmp(char str1[], char str2[])
{
// fill in your code here
return 0;
}
int comp201_strcmp(char str1[], char str2[])
{
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
{
cout << "str1 is shorter" << endl;
}
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
{
cout << "The contents of both strings are equal" << endl;
}
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
{
cout << "str1 is longer" << endl;
}
// fill in your code here
return 0;
}