Thread: My strcat

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    My strcat

    Hi All,
    I am a beginner of C Programming. I wrote a strcat function of my own. Please let me know your comments:

    Code:
    #include <stdio.h>
    
    
    void mystrcat(char *, char *, char *);
    
    
    int main()
    {
        char s1[] = "Hello";
        char s2[] = "World";
        char catstring[sizeof(s1) + sizeof(s2)];
           
        mystrcat(s1, s2, catstring);
        printf("%s", catstring);
        getch();
    }
    
    
    void mystrcat(char *s1, char *s2, char *catstring)
    {
        
        int i=0, k=0;
        
        while(*s1 != '\0')
        {
               catstring[k] = *s1;
               k++;
               s1++;
               
        }      
        
        while(*s2 != '\0')
        {
               catstring[k] = *s2;
               k++;
               s2++;
        }
        catstring[k] = '\0';
           
    }

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    What if catstring is passed as NULL?

    i is unused.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by ledow View Post
    What if catstring is passed as NULL?

    i is unused.
    Modified the code ... Please let me know if this seems ok ...

    Code:
    #include <stdio.h>
    
    
    void mystrcat(char *, char *, char *);
    
    
    int main()
    {
        char s1[] = "Hello";
        char s2[] = "";
        char catstring[sizeof(s1) + sizeof(s2)];
           
        mystrcat(s1, s2, catstring);
        printf("%s", catstring);
        if (*catstring == 0)
        {
            printf("String is Empty\n");
        }
        getch();
    }
    
    
    void mystrcat(char *s1, char *s2, char *catstring)
    {
        
        int k=0;
        
        while(*s1 != '\0')
        {
               catstring[k] = *s1;
               k++;
               s1++;
               
        }      
        
        while(*s2 != '\0')
        {
               catstring[k] = *s2;
               k++;
               s2++;
        }
        catstring[k] = '\0';
           
    }

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It still won't fix the problem if one of the strings are NULL, as you're still dereferencing the pointers without checking their validity.

    Also, your loops could be shortened significantly with no functional change:
    Code:
    while (*s1)
        catstring[k++] = *s1++;
    while (*s2)
        catstring[k++] = *s2++;

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by memcpy View Post
    It still won't fix the problem if one of the strings are NULL, as you're still dereferencing the pointers without checking their validity.

    Also, your loops could be shortened significantly with no functional change:
    Code:
    while (*s1)
        catstring[k++] = *s1++;
    while (*s2)
        catstring[k++] = *s2++;
    I changed the strings as follows:
    Code:
    char s1[] = "Hello";
    char s2[5];
    It seems to work fine. Also note that I am using following line in the code
    Code:
     catstring[k] = '\0';
    Should it not suffice to handle Null strings?

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > I changed the strings as follows:
    That's not going to help. 's2' isn't NULL, it's five bytes of stack memory that have no meaningful value assigned to them. Checking for NULL means that your function will not accept NULL as an argument:
    Code:
    mystrcat(s1, s2, NULL);
    > Should it not suffice to handle Null strings?
    No, all that does is null terminates the string. You need this, but it doesn't check for NULL.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Hmmm ... I understand ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat()
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 04:55 PM
  2. strcat
    By warney_out in forum C Programming
    Replies: 7
    Last Post: 10-01-2004, 01:01 AM
  3. strcat
    By linuxdude in forum C Programming
    Replies: 1
    Last Post: 12-11-2003, 04:55 PM
  4. Help with strcat
    By mmondok in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 06:33 PM
  5. strcat
    By dashing_76 in forum C Programming
    Replies: 3
    Last Post: 03-22-2002, 02:01 AM