Trying to write strcat function, kindly review the code and post the comments.

char* mystrcat(char* dest,const char* src)
	{
		char* ptr;
		ptr = dest;
		while(*dest++ != NULL);
		dest--;
		while(*dest++ = *src++);
		return ptr;
	}

Recommended Answers

All 3 Replies

Nice work :) Its just as unsafe as the original.

char* mystrcat(char* dest,const char* src,int sizeDest)
	{
		char* ptr = NULL;
		int sizeSrc = 0;
		
		while(src[sizeSrc] != NULL)
			sizeSrc++;
		
		if((sizeDest <= sizeSrc) && ((sizeDest - sizeSrc) >= sizeSrc))
			return ptr;
		
		ptr = dest;
		while(*dest++ != NULL);
		dest--;
		while(*dest++ = *src++);
		return ptr;
	}

line 9. Might make more sense to calculate the length of both the source and destination strings, then check if their sum is less than sizeDest variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.