How to find out the last character from a string?
Your technique with str[strlen(str) - 1] wonderful. As indicated, you should avoid repeated unnecessary calls to strlen and save the results.
I somehow think that this is not the right way, because strlen must iterate over the characters to get the length. So this operation will have O (n).
Repeated calls to strlen may be a C program error. However, you should avoid premature optimization. If the profiler actually demonstrates an access point where strlen is expensive, then you can do something similar for your literal string example:
const char test[] = "foo"; sizeof test
Of course, if you create a "test" on the stack, it carries little overhead (increasing / decreasing the stack pointer), but linear time is not involved.
Literal lines, as a rule, will not be so gigantic. For other cases, such as reading a large line from a file, you can save the line length in advance, as one example, to avoid recalculating the line length. It can also be useful as it will tell you in advance how much memory is allocated for your character buffer.
I have a string and you need to add char to it. How can i do this? strcat accepts only char *.
If you have a char and cannot output a string from it (char * c = "a"), then I believe that you can use strncat (you need to check for this):
char ch = 'a'; strncat(str, &ch, 1);
In the above code, the delimiter is a local variable that is destroyed after foo is returned. Is it possible to add it to a variable output?
Yes: functions such as strcat and strcpy make deep copies of the original string. They do not leave small pointers behind, so that the fine for local data will be destroyed after these operations.
If I concatenate two null terminating strings, strcat add two null terminating characters to the resulting string?
No, strcat will basically overwrite the null terminator on the dest line and write past it, and then add a new null terminator when it finishes.