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
  • Last Modified:

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
Asked:
MarcosInfo
  • 4
  • 4
1 Solution
 
sgvillCommented:
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
 
MarcosInfoAuthor 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
 
PaulCaswellCommented:
>>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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Paul
0
 
MarcosInfoAuthor Commented:
Paul thanks but this is a string with no null terminator
the strln determines the lenght of the string.
0
 
PaulCaswellCommented:
That's why I coded it using len1 and len2. Use strln1 or strln2 etc.

Paul
0
 
MarcosInfoAuthor Commented:
I see yes. sorry.. I had to read it a few times...
0
 
PaulCaswellCommented:
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
 
MarcosInfoAuthor 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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