435,482 Members | 3,157 Online
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

# How to Concatenate three strings

 P: 1 I'm confused on how to get the input of three strings and the concatenate them into one line Apr 23 '19 #1
5 Replies

 100+ P: 119 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" Apr 24 '19 #2

 Expert 100+ P: 2,400 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: Expand|Select|Wrap|Line Numbers char a = "Hello "; The C Standard allows initialized strings to reside in read-only memory. You will get a run-time error if you use strcat() or strncat() to write into read-only memory. Apr 24 '19 #3

 100+ P: 119 "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? Apr 24 '19 #4

 Expert 100+ P: 2,400 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? Expand|Select|Wrap|Line Numbers #define DEST_LEN 10   char dest[DEST_LEN+1]; 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 The periods represent slots in dest[] with uninitialized values. Apr 24 '19 #5

 100+ P: 119 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(). Apr 25 '19 #6