Concatenating strings in C

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 23

  • @nazhaassadi1361
    @nazhaassadi1361 3 года назад +7

    i usually don't understand these tutorials on youtube they talk very fast and keep copying and pasting stuff, yours is excellent thank you this helped me alot

  • @benbrinard007
    @benbrinard007 3 года назад +3

    the best c tutorial on RUclips!!!

  • @coolcodingcat
    @coolcodingcat Год назад +3

    //you should do:
    char* tmp = (char*)malloc(strlen(str1) + strlen(str2) + 1);
    strcpy(tmp, str1);
    strcat(tmp, str2);
    str1 = tmp;
    //consider making the above a function that accepts str1 and str2 and returns tmp and then assign its return value to str1
    //But now str1 is pointing to the heap and not the stack, so don't forget to do:
    free(str1);
    //When you are done with it.

    • @sarazar928ghost9
      @sarazar928ghost9 2 месяца назад

      But u have no limit here. Someone can type anything, like a string of 5000000 length

  • @coolcodingcat
    @coolcodingcat Год назад +1

    //Actually, just realized that my solution does not work with the code in this video, because apparently you cannot assign a pointer to an array in C, eventhough they are equivalents.
    //I also found that apparently setting a variable in the `[ ]` works, but I don't think it used to in the older version of C
    //So here is my solution that I came up with that works in older more classic C, that handles memory carefully and intelligently, without relying on C++:
    // And I realized that mystrcat has to return to a separate cstring, or it leaves a dangling pointer
    // Here is my solution
    #include
    #include
    #include
    char* mystrcpy(const char* const);
    char* mystrcat(const char* const, const char* const);
    int main()
    {
    char* str1 = mystrcpy("My name is ");
    char* str2 = mystrcpy("Bob Jordan");
    // careful, if you assign the return value of this to str1 or str2, it will leave a dangling pointer, so I assigned it to str3
    char* str3 = mystrcat(str1, str2);
    printf("%s
    ", str3);
    // cstrings str1, str2, and str3 are pointing to the heap, because they were set with malloc, so you have to free them
    // DO not free a cstring, if it was set using just square brackets [ ], because it is on the stack and it will automatically free
    // and in that case you would get a double freed pointer
    free(str1);
    free(str2);
    free(str3);
    return 0;
    }
    char* mystrcpy(const char* const str)
    {
    char* tmp = (char*)malloc(strlen(str));
    strcpy(tmp, str);
    return tmp;
    }
    char* mystrcat(const char* const str1, const char* const str2)
    {
    char* tmp = (char*)malloc(strlen(str1) + strlen(str2) + 1);
    strcpy(tmp, str1);
    strcat(tmp, str2);
    return tmp;
    }

  • @ravikrisihar3641
    @ravikrisihar3641 3 месяца назад

    Ur the best

  • @NavidRigiEbrahimi
    @NavidRigiEbrahimi Год назад

    what's the difference between strncat and strlcat?

    • @CodeVault
      @CodeVault  Год назад +1

      strlcat is a safer version of strncat although it's not part of the standard C library. Here's more details about strlcat and similar functions: linux.die.net/man/3/strlcat

    • @NavidRigiEbrahimi
      @NavidRigiEbrahimi Год назад

      @@CodeVault great. thanks.

  • @user-kc7ig7fy1s
    @user-kc7ig7fy1s 4 года назад +1

    HOW TO CONCATENATE MORE THAN 2 STRINGS ?

    • @CodeVault
      @CodeVault  4 года назад +1

      You can just concatenate the first 2 into 1 and then concatenate the 3rd into the result of that concatenation and so on

    • @user-kc7ig7fy1s
      @user-kc7ig7fy1s 4 года назад

      @@CodeVault thanks
      But I want to stock the result of the concatenation in other variable how to do that ? Plz

    • @CodeVault
      @CodeVault  4 года назад +4

      Hmm, let me give you an example with 2 strings:
      char str1[] = "This ";
      char str2[] = "is a ";
      char str3[] = "test.";
      char result[200];
      strcat(result, str1);
      strcat(result, str2);
      strcat(result, str3);
      printf("%s
      ", results); // Should print "This is a test"
      Note: Make sure you have enough characters in result to store all the characters from all the strings combined. Here I chose 200, but maybe you need way more than that... keep that in mind.

    • @user-kc7ig7fy1s
      @user-kc7ig7fy1s 4 года назад

      @@CodeVault THANK YOU SO MUCH SIR. I UNDERSTOOD.

    • @weeb3277
      @weeb3277 3 года назад

      @@CodeVault now can you do it with strcat_s or strncat?

  • @pieteregialfano2924
    @pieteregialfano2924 2 года назад

    Can we concat 2 string without using strcat?

    • @CodeVault
      @CodeVault  2 года назад +3

      Yes! You could simply do this using a for loop and copying character by character. Something like this:
      char str1[100] = "An";
      char str2[100] = " example";
      int i;
      int l1 = strlen(str1);
      int l2 = strlen(str2);
      for (i = l1; i < l1 + l2; i++) {
      str1[i] = str2[i - l1];
      }
      str1[l1 + l2] = 0; // Add the NULL terminator

  • @yohanesstefanus2597
    @yohanesstefanus2597 2 года назад

    how to concatenating a string more than 1 times into the same string?

    • @CodeVault
      @CodeVault  2 года назад +2

      Just call strcat multiple times

  • @taahahaha6604
    @taahahaha6604 2 года назад

    what mean is strLcat

    • @CodeVault
      @CodeVault  2 года назад +1

      strlcat is not a function in the C standard. It's just a reimplementation of strcat that does the same thing in a less error prone way. You can read more about it in the manual: linux.die.net/man/3/strlcat

    • @taahahaha6604
      @taahahaha6604 2 года назад

      @@CodeVault thank you