Thread: Opinions and/or suggestions...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Opinions and/or suggestions...

    I'm trying to write my own versions of strcat, strcpy and strcmp... They work fine right now it seems
    but I would appreciate any comments and/or suggestions how to make them better ( I assume they aren't perfect )
    if you know of good corrections which should be made.


    Code:
     
    //My own strcat...
    char* strcat(char text1[], char text2[])
    {
    	int i=0, j=0;
    	while ((int)text2[j] >0)
    	{
    		if ((int)text1[i] > 0)
    			i++;
    		else
    		{
    			text1[i] = text2[j];
    			i++;
    			j++;
    		}
    	}
    return text1;
    }
    
    
    //My own strcpy...
    
    char *strcpy(char text1[], char text2[])
    {
    	int k=0;
    	while ((int)text2[k] > 0)
    	{
    		text1[k] = text2[k];
    		k++;
    	} text1[k] = '\0';
    return text1;
    }
    
    
    // My own strcmp...
    
    bool strcmp(char text1[], char text2[])
    {
    	int i=0;
    	while ((int)text1[i] == (int)text2[i] && (int)text1[i] > 0)
    		i++;
    	if (text1[i] == text2[i])
    		return true;
    	else return false;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First, I'd change their names so they don't conflict with the standard functions. Other than that, the standard strcmp returns differently depending if the first string is before or after the second. Where your version just returns a boolean value. Also, the origional version retruns zero on true, and not one. So your truth test works the opposite of the standard.

    On an aside, unless you're trying to get around lint warnings or something, you don't need to typecast all of those items.

    I'll let some one else nit pick more, I just gave them a glance.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ok, thanks for your reply, I agree that they should be renamed, and about the return value of strcmp, I actually knew that but didn't think about it when I wrote the function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > strcat
    There is no point checking text2[j] >0 while you're busy finding the end of text1

    > (int)text2[j] >0
    This is implementation sensitive - an unqualified char may be signed or unsigned.
    If it's signed, and your string contains extended characters, your code will fail.
    It's better to use != 0 or == 0 for detecting the end of a string

    Perhaps work on the formatting as well - some lines are not indented consistently with other lines.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ok, i'll change the >0 to !=0 as it sounds like a much better idea, and btw thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  2. Opinions?
    By Decrypt in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2005, 05:29 PM
  3. Opinions on where to start?
    By mitchell_annix in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 10:18 PM
  4. Favors and Opinions
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-01-2003, 10:04 PM
  5. C++ Opinions
    By Cgawd in forum C++ Programming
    Replies: 15
    Last Post: 10-28-2002, 06:01 PM