C
C
Cyber ​​Cyber2021-12-08 09:00:39
C++ / C#
Cyber ​​Cyber, 2021-12-08 09:00:39

String problem in C: how to concatenate strings without library functions (old question removed)?

The task given to me is to "glue" two strings, which are initially given as an array of characters (in general, in C there is no need to do it differently). I tried to come up with a solution myself, but in the end I had to turn to the Internet, and this is what I found: code with source . How could the same thing be done, but with the help of traditional array processing? By the way, I can't use any library functions in my code, except for strlen().
(there was a problem with tags, advise the arrangement for the future, so that this does not happen again)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Antony, 2021-12-08
​​@KiberCuber

In general, the solution to the problem:
Take through strlen determine the lengths of strings A and B Select
an array of length strlen(A) + strlen(B) + 1
mempcy string A into a new array from the beginning, then memcpy string B into a new one with mixing by the length of string A , add '\0' to the end of the array.

By the way, I can't use any library functions in my code, except for strlen().

And even memcpy?
If you can't use library functions directly - make your own memcpy - it's really very simple.
In general, then it is not clear why strlen should not be banned - it is also not very difficult to implement it.

R
res2001, 2021-12-08
@res2001

How could the same thing be done, but with the help of traditional array processing?

And what is it about your link that is not traditional in terms of processing an array? There are quite traditional operations.
Apparently you are used to the fact that accessing array elements is arr[i]. But this is just one of the options.
The variant used in the code by reference - pointer increment - simply shifts the pointer to the next element of the array. In theory, this should work a little faster than arr[i].
By the way, the principle of operation of iterators in C++ std is exactly the same as used here. So you can think of dest and src as string iterators :-)
If you really want to, you can rewrite the code using indexing. There is nothing complicated in such code conversion.
By reference, an analogue of strcat is implemented, but this function (as well as the function by reference) can lead to array overruns, because there is no way to control the size of the arrays. It would be interesting to implement analogues of strncat or strncat_s.

C
CityCat4, 2021-12-08
@CityCat4

How could the same thing be done, but with the help of traditional array processing?

There is the most traditional processing of arrays - there is simply nowhere more traditional. An entry like a[b] imho may be an indicator that the author does not yet fully understand how C works with memory.
In general, an array is a mathematical abstraction. By itself, the notation a[1] = 2 has no particular meaning. But from the point of view of C - an array is a very specific thing and points to a specific memory cell from which it begins.
And here the most interesting begins. Since our "array" is a memory area, we can declare it to be any type. Let's cast to the type of structure - there will be an array of structures, cast to char - there will be a string :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question