I'm confused on how to get the input of three strings and the concatenate them into one line
How to Concatenate three strings
Collapse
X
-
Tags: None
-
You need to use the strcat() function twice.
Algorithm:
1) Input 1st string (let it be 'a')
2) Input 2nd string (b)
3) Input third one (c)
4) strcat(a, b)
5) strcat(a, c)
strcat(a, b) concatenates a and b and store the resultant string in a.
Let's say, a="Hello ", b="there ", c="cat"
After strcat(a, b) , a = "Hello there"
Then strcat(a, c) , a = "Hello there cat" -
After the input instructions (steps 1-3) above,- a points to an array of 7 chars ('Hello \0');
- b points to an array of 7 chars ('there \0'); and
- c points to an array of 4 chars ('cat\0').
The two strcat() calls (steps 4 and 5) will write 9 slots past the original position of the terminating null in the array pointed to by a. How do you know that array is large enough to hold those extra chars? If it isn't then you're overwriting other variables -- a classic buffer overflow bug.
You need a buffer large enough to hold the concatenated string. One approach is to use strlen() to compute how many characters there are (don't forget to count the terminating null) and then malloc() a big enough buffer. Another way is to statically allocate a buffer that ought to be big enough and then use strncat() to be sure not to overrun the end of that buffer.
If you use strncat(), then be careful with the num argument in successive calls. It needs to decrease as the concatenated string grows. Another issue with strncat() is that it may not write a terminating null to the output buffer. You should follow each call to strncat() with an assignment statement that stores null ('\0') in the last slot of the destination buffer.
Another issue arises if the first string is defined with an initializer:
Code:char a = "Hello ";
Last edited by donbock; Apr 24 '19, 07:24 PM. Reason: Added note that strncat may not write terminating null to destination.Comment
-
"Another issue with strncat() is that it may not write a terminating null to the output buffer. You should follow each call to strncat() with an assignment statement that stores null ('\0') in the last slot of the destination buffer."
I ran into an issue in past where strncat() messed up the concatenated string (showing some rekt characters after the concatenated string). However, I couldn't understand why such was happening. On going through resources, they say it writes null at the end. Like mentioned here.
Is this something compiler dependent or what?Comment
-
I see that you're right -- strncat always writes a terminating null; I guess the question is where does it put it.
Upon more careful review, I think this is how strncat works. Is this consistent with the weirdness you experienced?
Code:#define DEST_LEN 10 char dest[DEST_LEN[U]+1[/U]]; char s1 = "hello_"; // strlen(s1) = 6. char s2 = "world"; // strlen(s2) = 5. dest[0] = '\0'; // dest[]: \0 .......... strncat(dest, s1, DEST_LEN-strlen(dest)); // dest[]: h e l l o _ \0 .... strncat(dest, s2, DEST_LEN-strlen(dest)); // dest[]: h e l l o _ w o r l \0
Comment
-
Well I apologize, that was strncpy(), not the concatenate function that made the string produce unexpected output.
I just tested the strncat() and it works fine. Adds the null character at the end automatically.
While working with strncpy(), as mentioned in the example here, null character needs to be manually added in the last slot of the destination buffer. I was missing this thing earlier.
Thanks for the clarification on strncat().Comment
Comment