Best channel in C programming, thank you very much Dr for helping us ❤
@PortfolioCourses2 жыл бұрын
Thank you very much for the kind words Mohammed, and you're very welcome! :-)
@airrsongs Жыл бұрын
Love the channel. Technically, this isn't appending strings but rather joining strings. 😉 My simplified approach would be: char *join_strings(char *s1, char *s2) { int size = strlen(s1) + strlen(s2) + 1; char * buf = calloc(size, sizeof(char)); snprintf(buf, size, "%s%s", s1, s2); return buf; } snprintf uses the same format options as printf; the first argument is the destination buffer, the second argument is the number of characters to place in the buffer, for those wondering...
@PortfolioCourses Жыл бұрын
I’m glad you enjoy the channel, and thanks for sharing your code! :-) What is in this video is technically an append (or concatenation), though all these terms are used a bit loosely in practice. The word join for strings usually refers to an operation that joins together potentially multiple strings and may insert separator characters between them, like this: kzbin.info/www/bejne/sKauoomKl66Lhas
@airrsongs Жыл бұрын
@@PortfolioCourses I see your point, especially about the terms being used a bit loosely. Still, in my mind at least, append implies an in-place concatenation. But it's all good, I'll check out your other video! Also, thanks for all the content!
@staz8671 Жыл бұрын
Glad I bumped into this video. The concept is perfectly explained. Thank you 🙏
@PortfolioCourses Жыл бұрын
You’re welcome, I’m glad you found the video too! :-)
@TiagoSilva-wk6se9 ай бұрын
Correct me if I'm wrong, but I understood that you were going to append s2 to s1. I mean, the code appends perfectly and I learned quite a lot, but I tried to do an implementation where the function had void return and appended s2 to s1 instead and it was actually harder.
@justcurious194011 ай бұрын
Thanks for the video : I'm not sure but I think that the last line is not necessary (s[size-1] = '\0'; ) because Calloc does it for u 🙃 : char *string(char *s1, char *s2){ int s1_length = strlen(s1); int s2_length = strlen(s2); int size = s1_length + s2_length +1; char *s = calloc(size, sizeof(char)); for(int i = 0 ; i < s1_length ; i++) s[i] = s1[i]; for(int i = 0 ; i < s2_length ; i++) s[i+s1_length] = s2[i]; return s; }
@comoyun6 ай бұрын
everything is fine but white background really hurts my eyes especially at night
@sarker_sudi21 Жыл бұрын
Thank you for the explanation. I had one question. The function string_append receives two char pointer, but when u are passing in those parameters in the main you are passing in char arrays s1 and s2 instead of passing pointers (like char *s1 = "abc";). How does this still work ?
@mjsaedy6637 Жыл бұрын
In C when an array is passed as an argument to a function, it decays into a pointer. This means that inside the function it becomes a pointer.
@ElhamShadab Жыл бұрын
Hi , Is it possible to concatenate multiple strings in multithreading environment like below? 1) capture all strings in character pointer array variable 2) calculate the size of strings and store it in array 3) allocate dynamic memory using total size (after summation of array defined in point 2) 4) now call multiple threads to operate on different portion of buffer e.g. if buffer of 100 bytes were allocated then thread0 working for two strings starting from index 0 to index 19 (assuming some of bytes of both the strings are 20) and thread1 working on next two strings starting from index 20 to 39 (assuming some of bytes of both the strings are 20).....
@PortfolioCourses Жыл бұрын
That's probably possible Moahammad, though I'm not sure if that would really improve performance. With memory, accessing it using many threads instead of one, doesn't tend to improve performance, due to the way hardware works (we can think of all the threads as accessing memory through the same "pipe", even if we have more threads using the pipe, we have the same pipe with the same limits on speed, etc).
@alxbudn18 күн бұрын
why didnt you use malloc i wanna learn why
@mensaswede40288 ай бұрын
Where was this video when I was learning C as an 18-year-old university student in 1988? 😊
@comoyun6 ай бұрын
i am 18 and cs student and watching these videos with chatgpt explaining further and very lucky but lazy af
@benjamindreyer98842 жыл бұрын
What if the paramaters are also dynamically allocated on the heap, do you have to free them in order to avoid a memory leak?
@PortfolioCourses2 жыл бұрын
Yes, that's correct. :-)
@benjamindreyer98842 жыл бұрын
@@PortfolioCourses I played with it and wrote a function that works, its really much the same as yours. Thank you for explaining it so well!
@PortfolioCourses2 жыл бұрын
You’re welcome Benjamin! :-)
@danielatoche52742 жыл бұрын
hi, is it possible to allocate memory for a str that I get with scanf or gets?
@PortfolioCourses2 жыл бұрын
Great question Daniel! :-) Yes, but you would need to either dynamically allocate the memory *before* calling scanf or gets, OR you could use a regular char array with scanf or gets and then after copying the string from the char array to space that you dynamically allocate. If you use the 2nd approach, you could initially make the char array very large, but then create only the amount of space need for the dynamically allocated memory based on the string length entered. Maybe something like this: char buffer[4096]; scanf("%s", buffer); char *string = malloc(sizeof(char) * (strlen(buffer) + 1)); strcpy(string, buffer); Then you're only using the amount of space necessary to store the string, and no more than that.
@danielatoche52742 жыл бұрын
@@PortfolioCourses thank you very much!
@PortfolioCourses2 жыл бұрын
@@danielatoche5274 You're very welcome! :-D
@phililezondi8813 Жыл бұрын
how do you append without using calloc
@PortfolioCourses Жыл бұрын
Great question Philile! :-) I'm assuming that you mean "how do you append without using dynamic memory allocation (malloc, calloc, realloc)". You could use the built-in strcat() function: kzbin.info/www/bejne/amHcgZyDeMycrrs. Though you would need to make sure that you destination char array was large enough to hold the concatenated string. You could also make your own strcat() function like this, if you wanted to see how it can be implemented: kzbin.info/www/bejne/qH6yd2ton6ikatE/. Hopefully this helps!