Home > Enterprise >  Function to concatenate two strings in C using strlen but without using strcat
Function to concatenate two strings in C using strlen but without using strcat

Time:02-25

I need to concatenate the strings "abc" and "def" using strlen but without using strcat. Can someone show me what is wrong with my main() function?

#include <stdio.h>
#include <string.h>

void strconcatenate(char *string1, char *string2) {
    int i;
    int j = strlen(string1);
    for (i = 0; string2[i]; i  ) {
        string1[i j] = string2[i];
    }
    string1[i   j]= '\0';
}

int main() {
    char string1[3] = "abc";
    string1[strlen(string1) - 1] = '\0';
    char string2[3] = "def";
    string2[strlen(string2) - 1] = '\0';

    strconcatenate(string1, string2);  
    printf("Resultant string = %s\n", string1);
    return 0;
}

CodePudding user response:

In main you declared character arrays with three elements

char string1[3] = "abc";
//...
char string2[3] = "def";

These arrays do not contain strings because they do not store the terminating zero character '\0'. The first array contains three characters { 'a', 'b', 'c' } and the second array contains these three characters { 'd', 'e', 'f' }.

Then you applied the standard string function strlen to these arrays which expects that passed to it arrays contains strings. That is the function calculates the number of characters in a string by counting characters until the terminating zero character is encountered. As the arrays do not contain the terminating zero character '\0' that is as the arrays do not contain strings the calls of strlen invoke undefined behavior.

If you want to append one string to another string then the destination character array shall have enough size to be able to accommodate the second string.

Thus the function main can look the following way

int main( void ) {
    char string1[7] = "abc";
    char string2[] = "def";

    printf( "Resultant string = %s\n", strconcatenate( string1, string2 ) );

    return 0;
}

Now the both arrays string1 and string2 contain strings. Moreover the array string1 reserved enough space to accommodate the string stored in the array string2.

The declaration of the function strconcatenate should be similar to the declaration of the standard C function strcat.

char * strconcatenate( char *string1, const char *string2 );

That is the second parameter should have the qualifier const because the passed array is not changed within the function and the function should return the destination array that will contain the concatenated strings.

The function definition will look the following way

char * strconcatenate( char *string1, const char *string2 ) 
{
    for ( char *p = string1   strlen( string1 ); *p   = *string2  ; );

    return string1;
}

CodePudding user response:

There are multiple problems in your code:

  • [major] char string1[3] = "abc"; defines the destination array with a size of 3 bytes which does not have a enough space for the string "abc" including its null terminator and definitely not long enough to receive the extra characters from string2 at the end. Change this to char string1[7] = "abc";
  • [major] char string2[3] = "def"; defines the source array with a size of 3 bytes which does not have a enough space for the string "def" including its null terminator, hence will not be null terminated.
  • [major] string1[strlen(string1) - 1] = '\0'; overwrites the last character of abc, this not necessary.
  • [major] same remark for string2[strlen(string2) - 1] = '\0';
  • [minor] string2 should be defined as const char * in void strconcatenate(char *string1, char *string2)
  • [minor] i and j should be defined with type size_t
  • [minor] for compatibility with the standard function strcat, strconcatenate should have a return type of char * and return the pointer to the destination array. Yet, As commented by Jonathan Leffler, a more useful design for the return value is to return a pointer to the null at the end of the concatenated string. You already know where the start of the string is; that isn't very interesting information. But knowing where the end of the string is after concatenation — that is useful information which only the concatenate function is privy to. –

Here is a modified version:

#include <stdio.h>
#include <string.h>

char *strconcatenate(char *string1, const char *string2) {
    char *p = string1   strlen(string1);
    while ((*p = *string1) != '\0') {
        string1  ;
        p  ;
    }
    return string1;  /* strcat semantics */
    //return p;      /* alternative semantics */
}

int main() {
    char string1[7] = "abc";
    char string2[] = "def";

    strconcatenate(string1, string2);
    printf("Resultant string = %s\n", string1);
    return 0;
}

CodePudding user response:

Since string1 and string2 has 3 characters so you need to declare it with 4 bytes('\0') and also when you need not to subtract 1 from strlen because it points to '\0' character. Below is the change to the code.

int main()
{
  char string1[4] = "abc";
  string1[strlen(string1)] = '\0';
  char string2[4] = "def";
  string2[strlen(string2)] = '\0';

  strconcatenate(string1,string2);  
  printf("Resultant string = %s\n",string1);
  return 0;
}
  • Related