C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 8th, 2004, 11:10 AM
Bespin Bespin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 4 Bespin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
concatenate two strings - newbie help!

Ive taken the following program from a textbook im working through....

// Concatenate - concatenate two strings with a " - " in the middle


#include <stdio.h>

#include <iostream.h>

// the following include file is required for the str functions

#include <string.h>



// prototype declarations

void concatString(char szTarget[], char szSource[]);



int main (int nArg, char* pszArgs[])

{

//read the first string

char szString1[256];

cout << "Enter string #1: ";

cin.getline(szString1, 128);



//now get the second string

char szString2[128];

cout << "Enter String #2: ";

cin.getline(szString2, 128);



//concatenate a "-" onto the first... (point1)

concatString(szString1, " - ");

//strcat (szString1, " - ");



//now add the second string (point2)

concatString(szString1, szString2);

//strcat (szString1, szString2);



//and display the result

cout << "\n" << szString1 << "\n";



return 0;



}

//concatString - concatenate the szSource string on to the end of the szTarget

//string



void concatString(char szTarget[], char szSource[])

{

// Find the end of the first string

int targetIndex =0;

while (szTarget[targetIndex]) // (point3)

{

targetIndex++;

}

//tack the second on to the end of the first

int sourceIndex=0;

while (szSource[sourceIndex]) // (point4)

{

szTarget[targetIndex] = szSource[sourceIndex];

targetIndex++;

sourceIndex++;

}

//tack on the terminating null

szTarget[targetIndex]='\0';

}


I would be really grateful if someone could clarify the code ive marked in brackets as points 1-4.
I think I understand 80% of what this program is doing, I just cant work out how its doing it - if
that makes any sense? on point1, the line "concatString(szString1, " - ");" tells me that its going to
run the concatString function, using the values in szString1 and for some reason, a minus sign.
On point2, it seems to do exactly the same thing, though using the values of szString1 and szString2. Is it
running the concatString function twice, or am I cracking up?
On points 3 and 4, I cant see how the "while" aspects are working as there are no conditions? in the first
example, it sets targetIndex to 0 and then I simply do not understand what it is doing from that
point onwards!


I would be most grateful if someone could explain this to a newbie who is going to throw this textbook
out of the window!!!

Cheers,
Mike.



Reply With Quote
  #2  
Old August 8th, 2004, 01:28 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 15
This program concatenates 2 strings together with a " - " sign in between them. The function is first called to append the " - " on the end of the first string. Then the function is called again to append the second string on the end.

So if our strings are "first" and "second" then the function is first called with "first" and " - " so we have strings "first - " and "second". When it is called the second time we get "first - second" as the result.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old August 9th, 2004, 01:16 PM
Bespin Bespin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 4 Bespin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it does thanks!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > concatenate two strings - newbie help!


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap