TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: VICTOR KEMP
from: DARIN MCBRIDE
date: 1998-01-18 21:42:00
subject: char to string

 VK> Hi, maybe that last char to char * question I asked in 
 VK> this echo was so simple that no-body bothered to reply.
Don't recall seeing it...
 VK> Anyway I need to append a char onto the end of a 
 VK> string, something like this
 VK> char stringvar[90];
 VK> char charvar;
 VK> strcat(stringvar, charvar);
 VK> But that doesn't work, someone please tell me how it's done.
Ah, yes, that wouldn't work.  There are a couple ways of doing it.  First, 
the "tricky" way (IMO).  Do this and you will awe some programmers, and annoy 
the heck out of others.  :-)
char string_to_append[2] = { charvar, '\0' };
strcat(stringvar, string_to_append);
The other way is to do this by hand (you could make a function out of it):
int i = strlen(stringvar); // 1
stringvar[i++] = charvar;  // 2
stringvar[i  ] = '\0';     // 3
Note that if I wanted to add another character - say charvar2 - after 
charvar, I would add the following line between 2 and 3:
stringvar[i++] = charvar2; // 2.1
This is why I use the ++ - to continue incrementing our position.
NOTE: This isn't C++ specific - you may get a better audience in the C echo 
(C_ECHO).
C++ answer: If you were using C++-style strings, you could simply use the +=:
string strVar;
// ...
strVar += charvar;
// strVar += charvar2; etc...
Hope this helps.
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.