Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 303

# concatenating 2 strings in a function with limit of 80 char in first string

I have a function that is concatenating 2 strings (string 1 and string 2)
Both strings have up to 80 chars ( no null terminator) the srtlen (string length) tells how many indexes are in the string.

I need to check 1) How long is first string a) if 80 already then nothing to append no room
b) if less then 80 then procede

b) IF LESS THEN 80
then I know would need the string 1 length - 80 to give what indexs are left. then know where the first position is to add from String 2 .. String 2 can only add as many chars up til last index filled in.

THIS IS WHAT I HAVE SO FAR BUT NOT SURE IN RIGHT PATH

//concatenates s onto value and returns result
String concat(const String &s)
{
int ndx, ndx2, emptyndx;

emptyndx = strln-80 //this is how many chars are empty in 1st string
NOT SURE HERE>

//now perform a loop to bring the index to last character in the object's value
for (ndx = 0; ndx < strln; ++ndx);

//now perform another loop to copy the passed-in string's value to the object's
for (ndx2 = 0; ndx2 < s.strln && emptyndx > 0; ++ndx,++ndx2,--emptyndx )
strval[ndx] = s.strval[ndx2];

//now null terminate the first string
//            strval[ndx] = '\0';

//and also update the length
strln = ndx;

//instantiate a new object with strval and return it
return String(strval);
}
0
MarcosInfo
• 4
• 4
1 Solution

Commented:
The way you calculate emptyndx will give you a negative number. (say you have a string length 10.  then 10-80 will make emptyndx= -70.  You probably meant to say 80-strln
0

Author Commented:
Oh yes..thanks.. does the logic follow for rest? should it be
for (ndx2 = 0; ndx2 < s.strln && emptyndx > 0; ++ndx,++ndx2,--emptyndx )
&& or ||
that is the part that confuses me
I know I want not copy past strln of 2nd string but do not want to copy past availabe empty indexes in string 1
0

Commented:
>>I know I want not copy past strln of 2nd string but do not want to copy past availabe empty indexes in string 1
Surely, this statement translates directly into C as:

for ( i = len1, j = 0; i < 80 && j < len2; i++, j++ )
{
str1[i] = str2[j];
}
str1[i] = '\0';

I am sure you can adjust this code to fit your variable names. Note here that i encode the algorithm and then apply that code to the data. Doing it the other way around is a common mistake and often produces unclear code much like you posted.

Paul
0

Commented:
P.S. You may find (80-1) should be used instead of 80 to allow room for the terminator.

Paul
0

Author Commented:
Paul thanks but this is a string with no null terminator
the strln determines the lenght of the string.
0

Commented:
That's why I coded it using len1 and len2. Use strln1 or strln2 etc.

Paul
0

Author Commented:
I see yes. sorry.. I had to read it a few times...
0

Commented:
The essence here is to code it so that it CANNOT go wrong:

1. for ( i = len1, j = 0
Start at the end of the first string and the start of the second.

2.  i < 80 && j < len2
Stop when we've reached 80 characters on the first string or the end of the second.

3.  str1[i] = str2[j];
Copy one character each time around.

4. i++, j++
Step forward by one character each time around.

Clearly, nothing can go wrong because you have encoded exactly what you have described.

Paul
0

Author Commented:
Yes, you explaing now makes it much easier to undertand.
I am so new to this, I can understand what I want done but getting In C++ and understanding the conditions if met True or False gets me confused with && or ||
0

## Featured Post

• 4
• 4
Tackle projects and never again get stuck behind a technical roadblock.