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
@benbrinard0073 жыл бұрын
the best c tutorial on KZbin!!!
@coolcodingcat Жыл бұрын
//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.
@sarazar928ghost93 ай бұрын
But u have no limit here. Someone can type anything, like a string of 5000000 length
@coolcodingcat Жыл бұрын
//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; }
@ravikrisihar36414 ай бұрын
Ur the best
@NavidRigiEbrahimi Жыл бұрын
what's the difference between strncat and strlcat?
@CodeVault Жыл бұрын
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 Жыл бұрын
@@CodeVault great. thanks.
@user-kc7ig7fy1s4 жыл бұрын
HOW TO CONCATENATE MORE THAN 2 STRINGS ?
@CodeVault4 жыл бұрын
You can just concatenate the first 2 into 1 and then concatenate the 3rd into the result of that concatenation and so on
@user-kc7ig7fy1s4 жыл бұрын
@@CodeVault thanks But I want to stock the result of the concatenation in other variable how to do that ? Plz
@CodeVault4 жыл бұрын
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-kc7ig7fy1s4 жыл бұрын
@@CodeVault THANK YOU SO MUCH SIR. I UNDERSTOOD.
@weeb32773 жыл бұрын
@@CodeVault now can you do it with strcat_s or strncat?
@pieteregialfano29242 жыл бұрын
Can we concat 2 string without using strcat?
@CodeVault2 жыл бұрын
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
@taahahaha66043 жыл бұрын
what mean is strLcat
@CodeVault3 жыл бұрын
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
@taahahaha66042 жыл бұрын
@@CodeVault thank you
@yohanesstefanus25973 жыл бұрын
how to concatenating a string more than 1 times into the same string?