?
Solved

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

Posted on 2005-03-02
9
Medium Priority
?
300 Views
Last Modified: 2010-04-17
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
Comment
Question by:MarcosInfo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:sgvill
ID: 13439078
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 Comment

by:MarcosInfo
ID: 13439134
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13439393
>>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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 16

Expert Comment

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

Paul
0
 

Author Comment

by:MarcosInfo
ID: 13439428
Paul thanks but this is a string with no null terminator
the strln determines the lenght of the string.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13439599
That's why I coded it using len1 and len2. Use strln1 or strln2 etc.

Paul
0
 

Author Comment

by:MarcosInfo
ID: 13439684
I see yes. sorry.. I had to read it a few times...
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 2000 total points
ID: 13439785
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 Comment

by:MarcosInfo
ID: 13439824
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question