473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to Concatenate three strings

I'm confused on how to get the input of three strings and the concatenate them into one line
Apr 23 '19 #1
5 1724
dev7060
636 Expert 512MB
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
donbock
2,426 Expert 2GB
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
  1. 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
dev7060
636 Expert 512MB
"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
donbock
2,426 Expert 2GB
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
  1. #define DEST_LEN 10
  2.  
  3. char dest[DEST_LEN+1];
  4. char s1 = "hello_";  // strlen(s1) = 6.
  5. char s2 = "world";   // strlen(s2) = 5.
  6.  
  7. dest[0] = '\0';
  8. // dest[]: \0 ..........
  9. strncat(dest, s1, DEST_LEN-strlen(dest));
  10. // dest[]: h e l l o _ \0 ....
  11. strncat(dest, s2, DEST_LEN-strlen(dest));
  12. // dest[]: h e l l o _ w o r l \0
The periods represent slots in dest[] with uninitialized values.
Apr 24 '19 #5
dev7060
636 Expert 512MB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: OPQ | last post by:
Hi all, I'd happy to have you share some thougts about ultimate optimisations on those 2 topics: (1)- adding one caractere at the end of a string (may be long) (2)- in a dict mapping a key...
2
by: VMI | last post by:
In my datatable, I'm retrieving three fields from an Access table and I concatenate them into one string. Then I put this information in another datacolumn that I added to the datatable (with...
1
by: alternativa | last post by:
Hello, I'm dealing with a problem concerning strcat.. I would like to concatenate 3 strings, which in fact are integer numbers. My idea is to write something like this: void main (void); {...
5
by: Mr. Ken | last post by:
In my function, my main calls a function to save the data. In script-based languages like matlab, I can use multiple in strcat. However, in C++ it's not working since first argument of strcat...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
4
by: roxorsoxor2345 | last post by:
I have this custom string class that I have created. #include <cstdlib> #include <iostream> using namespace std; class Cstring { private:
4
by: ultragc | last post by:
Hi all. I am starting on javascript and need some help. Basically, I have a form in which I need to compare three text fields (string1, string2, and string3). The user must enter the exact text on...
6
by: Elamathi | last post by:
I just wanted to concatenate 3 strings addr1,addr2,addr3 and should be placed again in addr1 help me
4
by: dissectcode2 | last post by:
Hi - please help. I have two strings and a number which I need to concatenate together and I'm confused to what is the best approach. Here is my code: char *s1 = "iamafilename_"; char *s2 =...
1
by: Lesley Hagerty | last post by:
I am on windows 7 developing an application in visual basic 2006 when I try and concatenate 2 strings using & - I get object not found ? have a got a reference missing if so can anyone please tell...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.