String Compare problem

a C function to copy one string into another

prototype:
void myStrCpy(char[], const char[]);

I need to write this function in C++ but can't use <cstring> and pointer.

What I wrote is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void myStrCpy(char[], const char[]);
int main() 
{
	char *cpy="World";
	const char *str = "Hello";
	myStrCpy(cpy, str);
}

void myStrCpy(char cpy[], const char str[])
{
	int i;
	int temp;
	for(i = 0; str[i] != '\0';i++)
	{
		temp = str[i];
		cpy[i]=temp;
	}
}



The compiler VS2012 can't find error, but the program is not working :(
Can anyone tell me what's wrong?
Last edited on
Change char *cpy="World"; to char cpy[] = "World";
thank you very much! It's working good!
Could you tell me why the first one is wrong?
char * x = "Some string literal"; makes a pointer x that points to (read-only) memory where "Some string literal" resides. Obviously, you shouldn't attempt to modify that memory.

char x[] = "Some string literal"; creates an array of sufficient size and copies the string literal to that array.
OK! get it! Thank you very much!
I find a problem of this program. This is my final version:
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
void myStrCpy(char[], const char[]); 

int main() 
{
	char text[128];
	cin.getline(text, 128);

	char abc[]="ABC";

	cout <<"string: "<<text<<endl;

	if (myStrCmp(text, abc)==0)
	{
		cout<<"Your string equals ABC"<<endl<<endl;
	}
	else 
	{
		cout<<"Your string does not equal ABC"<<endl<<endl;
	}
}

void myStrCpy(char text[], const char abc[])
{
	int i;
	int temp;
	for(i = 0; abc[i] != '\0';i++)
	{
		temp = abc[i];
		text[i]=temp;
	}
}


If the Input string length is shorter than ABC, it will give a right compared answer,

AB
string: AB
Your string does not equal ABC


If the input string length is longer than ABC, such as ABCD, since the program will quit working when it see the "\0" of the "ABC", it won't know there is one more "D" there, the output will become:

string: ABCD
Length: 4
Your string equals ABC

which is apparently wrong.

But how to fix it?
Last edited on
You aren't using myStrCopy in that snippet.
damn...well...but i think both of myStrCpy and myStrcmp have same problem

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
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//prototype
void myStrCpy(char[], const char[]); 
int myStrCmp(const char[], const char[]);

int main() 
{
	char text[128];
	cin.getline(text, 128);

	char abc[]="ABC";

	if (myStrCmp(text, abc)==0)
	{
		cout<<"Your string equals ABC"<<endl<<endl;
	}
	else 
	{
		cout<<"Your string does not equal ABC"<<endl<<endl;
	}

	myStrCpy(text, "ABC");
	cout<<"String: "<<text<<endl;
	if (myStrCmp(text, abc)==0)
		cout<<"The changed string equals ABC"<<endl<<endl;
	else 
		cout<<"The changed string does not equal ABC"<<endl<<endl;
}

void myStrCpy(char text[], const char abc[])
{
	int i;
	int temp;
	for(i = 0; abc[i] != '\0';i++)
	{
		temp = abc[i];
		text[i]=temp;
	}
}

int myStrCmp(const char text[], const char abc[])
{
	int i;
	int res;
	int temptext,tempabc;
	for(i = 0; abc[i] != '\0';i++)
	{
		temptext=text[i];
		tempabc=abc[i];
		res=tempabc-temptext;
		if (res!=0)
		{
			return 1;
		}
	}
	return 0;
}




myStrcpy can only replace the string length of "ABC" at beginning, the rest of the length will just be ignored.
Last edited on
1
2
3
4
5
6
7
8
9
void myStrCpy(char text[], const char abc[])
{
    // no need for temp variable.
    int i ;
    for(i = 0; abc[i] != '\0';i++)
        text[i] = abc[i] ;   

     text[i] = '\0' ;  // c-strings end with a nul character.
}
Thank you very much!

myStrCpy is working fine now
Can anyone give hints about myStrCmp?

I think I shouldn't let the function stop at finding '\0'
But where should I put checking null character then? And what should I put to stop comparing?
Last edited on
I would suggest using a while loop for myStrCmp.

And looping on (text[i] != '\0' && abc[i] != '\0').
OK, let me try that one. And I make this one as a new topic to avoid confusion. :\
The null character is equal to zero. Since c++ (and c) treat non-zero as true, checking for a null terminator can be done like so:
1
2
3
4
5
6
7
int myStrlen(const char* string)
{
  int i = 0;
  while (string[i])
    i++;
  return i;
}


There is no need to check both strings for null terminators (in fact, you will still get the same issue) because the loop would inherently return in the case that "text" is shorter than "abc"
1
2
3
4
5
6
7
//...
res=tempabc-temptext;
if (res!=0)
{
  return 1;
}
//... 


The simplest solution is to adjust the code after the loop. The loop has ended because abc[i] == null terminator. At that point, if text[i] also == null terminator, then you can say that the strings are equal.

I'm a little surprised the teacher said "no pointers", but that's another story.
Last edited on
Thank you for helping.

What's myStrlen about here?

I actually have a myStrlen function in the program, as another array, for getting the length of the strings. But that's another assignment requirement. The whole program has 3 functions actually. myStrlen, myStrcmp and myStrcpy

And if i delete this part,
1
2
3
4
5
6
7
8
//...
res=tempabc-temptext;
if (res!=0)
{
  return 1;
}
//... 


How can I test whether they are same or not?
The simplest solution is to adjust the code after the loop. The loop has ended because abc[i] == null terminator. At that point, if text[i] also == null terminator, then you can say that the strings are equal.


Not quite true. Suppose text[] is shorter than abc and the character after the text (assuming it's even memory safe to access and doesn't cause a segmentation fault) is a nul character? That's why you do need to check both strings for termination.
Last edited on
Topic archived. No new replies allowed.