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
//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.
//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; }
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
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.
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
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
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
the best c tutorial on RUclips!!!
//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.
But u have no limit here. Someone can type anything, like a string of 5000000 length
//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;
}
Ur the best
what's the difference between strncat and strlcat?
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
@@CodeVault great. thanks.
HOW TO CONCATENATE MORE THAN 2 STRINGS ?
You can just concatenate the first 2 into 1 and then concatenate the 3rd into the result of that concatenation and so on
@@CodeVault thanks
But I want to stock the result of the concatenation in other variable how to do that ? Plz
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.
@@CodeVault THANK YOU SO MUCH SIR. I UNDERSTOOD.
@@CodeVault now can you do it with strcat_s or strncat?
Can we concat 2 string without using strcat?
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
how to concatenating a string more than 1 times into the same string?
Just call strcat multiple times
what mean is strLcat
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
@@CodeVault thank you