Concatenating strings in C

  Рет қаралды 20,000

CodeVault

CodeVault

Күн бұрын

Пікірлер: 23
@nazhaassadi1361
@nazhaassadi1361 3 жыл бұрын
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 жыл бұрын
the best c tutorial on KZbin!!!
@coolcodingcat
@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.
@sarazar928ghost9
@sarazar928ghost9 3 ай бұрын
But u have no limit here. Someone can type anything, like a string of 5000000 length
@coolcodingcat
@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; }
@ravikrisihar3641
@ravikrisihar3641 4 ай бұрын
Ur the best
@NavidRigiEbrahimi
@NavidRigiEbrahimi Жыл бұрын
what's the difference between strncat and strlcat?
@CodeVault
@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
@NavidRigiEbrahimi Жыл бұрын
@@CodeVault great. thanks.
@user-kc7ig7fy1s
@user-kc7ig7fy1s 4 жыл бұрын
HOW TO CONCATENATE MORE THAN 2 STRINGS ?
@CodeVault
@CodeVault 4 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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
@taahahaha6604
@taahahaha6604 3 жыл бұрын
what mean is strLcat
@CodeVault
@CodeVault 3 жыл бұрын
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
@yohanesstefanus2597
@yohanesstefanus2597 3 жыл бұрын
how to concatenating a string more than 1 times into the same string?
@CodeVault
@CodeVault 3 жыл бұрын
Just call strcat multiple times
Replace substrings in C
23:04
CodeVault
Рет қаралды 25 М.
What are variadic functions (va_list) in C?
13:49
CodeVault
Рет қаралды 24 М.
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 6 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 32 МЛН
C_67 C Program to concatenate two strings | with strcat() and without strcat()
21:30
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
you will never ask about pointers again after watching this video
8:03
How to Write Function-Like Preprocessor Macros (C example)
13:59
Jacob Sorber
Рет қаралды 43 М.
strtok() function | C Programming Tutorial
12:36
Portfolio Courses
Рет қаралды 64 М.
How I program C
2:11:32
Eskil Steenberg
Рет қаралды 781 М.
F-strings In Python: Everything You Need To Know
23:29
ArjanCodes
Рет қаралды 50 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 404 М.
Difference between memmove and memcpy
9:15
CodeVault
Рет қаралды 26 М.
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН