stringg

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>
using namespace std;

int comp201_strcmp(char str1[], char str2[]);

int main()
{

}
Last edited on
Here is a hint..

use

strcmp in your function.

http://www.cplusplus.com/reference/cstring/strcmp/

take advantage of of the return values of strcmp

strcmp - Return Values

<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
Are you confused on how to use the function:
int comp201_strcmp(char str1[], char str2[]);?
Or what question do you specifically have?
I just dont know how to use the char str1[] and char str2 [].

I already have this:

int comp201_strcmp(char str1[], char str2[]);
bdanielz wrote:
strcmp in your function.

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".



Last edited on
Okay, so can someone help me. What should I have? Do I need int main()? How do you declare char str1 and char str2 after the first line I have? Thanks
My bad, I forgot to include this. Now I only need help where the comment is, "fill in your code here"

Thanks

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
#include <iostream>
using namespace 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;
}
We're not going to write the code for you. You need to figure it out for your self.
I gave you a good hint above:
Think about the algorithm of how to compare two strings. You want to iterate through the two strings comparing each respective character.


Yes, this is what I was thinking.....

1
2
3
4
5
6
7
8
9
int comp201_strcmp(char str1[], char str2[])
{ 
	if (char str1[] < char str2[])
	{

	}
	
	return 0;
}
This is what i have now..... Please help, still confused.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}
okay, seems like my conditional statements are wrong??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int comp201_strcmp(char str1[], char str2[])
{ 
	for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
	{
		if (str1[i] == '\0' && str2[i] == '\0')
		{
			cout << "str1 is shorter." << endl;
			break;
		}
		else if (str1[i] == '\0')
		{
			cout << "The contents of both strings are equal." << endl;
			break;
		}
		else if (str2[i] == '\0')
		{
			cout << "str1 is longer." << endl;
			break;
		}
		else
		{
			continue;	// bypass section of code and begin next iteration
		}
	}
Line 5 is testing if the strings are the same length. i.e. The null terminator is in the same place.

At no time are you actually comparing the characters in the respective strings.

Line 25: If you've reached the end of both strings with no mis-compares, then the strings are equal and you should return 0.

So what should I do in line 5?

Did I add the return in right spot?

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
int comp201_strcmp(char str1[], char str2[])
{ 
	for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
	{
		if (str1[i] == '\0' && str2[i] == '\0')
		{
			cout << "str1 is shorter." << endl;
			break;
		}
		else if (str1[i] == '\0')
		{
			cout << "The contents of both strings are equal." << endl;
			break;
		}
		else if (str2[i] == '\0')
		{
			cout << "str1 is longer." << endl;
			break;
		}
		else
		{
			continue;	// bypass section of code and begin next iteration
		}
            return 0;
	}
So this is what I now have. Do you think according to the problem I need to add any cout statements, or is this it?

Thanks.

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
#include <iostream>
using namespace 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[])
{ 
	for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
	{
		if (str1[i] != str2[i])
		{
			if (str1[i] < str2[i])
				return -1;
			else if (str1[i] > str2[i])
				return 1;
		}
		else if (str1[i] == '\0' && str2[i] == '\0')
		{
			return 0;
		}
	}
	return 0;
	// fill in your code here
}
I believe I am set.
If the strings are different lengths, you're going to overrun the end of the shorter string.
I want to change the for loop to a do-while loop, how can I do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int comp201_strcmp(char str1[], char str2[])
{ 
	for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
	{
		if (str1[i] != str2[i])		
		{
			if (str1[i] < str2[i])
				return -1;				
			else if (str1[i] > str2[i])
				return 1;				
		}
		else if (str1[i] == '\0' && str2[i] == '\0')	
		{
			return 0;					
		}
	}
	return 0;			
}
1
2
3
4
5
  i = 0;  //  Initialize loop variable
  while (! termination_condition)
  {  // loop logic
     i++;  // increment loop variable
  }

Topic archived. No new replies allowed.