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...
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: ruclips.net/video/zuKqVTbIXOI/видео.html
@@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!
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.
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; }
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 ?
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).....
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).
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.
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: ruclips.net/video/40yPiMCgixY/видео.html. 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: ruclips.net/video/rMOF82jCq4o/видео.html/. Hopefully this helps!
Best channel in C programming, thank you very much Dr for helping us ❤
Thank you very much for the kind words Mohammed, and you're very welcome! :-)
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...
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: ruclips.net/video/zuKqVTbIXOI/видео.html
@@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!
Glad I bumped into this video. The concept is perfectly explained. Thank you 🙏
You’re welcome, I’m glad you found the video too! :-)
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.
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;
}
everything is fine but white background really hurts my eyes especially at night
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 ?
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.
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).....
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).
why didnt you use malloc i wanna learn why
Where was this video when I was learning C as an 18-year-old university student in 1988? 😊
i am 18 and cs student and watching these videos with chatgpt explaining further and very lucky but lazy af
What if the paramaters are also dynamically allocated on the heap, do you have to free them in order to avoid a memory leak?
Yes, that's correct. :-)
@@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!
You’re welcome Benjamin! :-)
hi, is it possible to allocate memory for a str that I get with scanf or gets?
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.
@@PortfolioCourses thank you very much!
@@danielatoche5274 You're very welcome! :-D
how do you append without using calloc
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: ruclips.net/video/40yPiMCgixY/видео.html. 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: ruclips.net/video/rMOF82jCq4o/видео.html/. Hopefully this helps!