views:

73

answers:

4

Dear all,
How can I add an integer variable to a string and char* variable? for example:
int a = 5;
string St1 = "Book", St2;
char *Ch1 = "Note", Ch2;

St2 = St1 + a --> Book5
Ch2 = Ch1 + a --> Note5

Thanks

A: 

You need to create another string large enough to hold the original string followed by the number (i.e. append the character corresponding to each digit of the number to this new string).

dirkgently
+2  A: 

The C++ way of doing this is:

std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();

You can also do the same thing with Ch1:

std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());
Andreas Brinck
`new char[temp.str().length() + 1];` is not very C++ish, you know - it's an extension used by GCC.
LiraNuna
Several errors appear, first of them is about the first line.
aryan
@LiraNuna Hmm, in what way is that a GCC-extension? It just `news` an array of chars. (I'm using VC2005/2008 but the code works fine in Comeau as well, without extensions)
Andreas Brinck
@aryan Try `#include <sstream>`
Andreas Brinck
I add <sstream> and now my code is working!!! Thank you very much
aryan
+1  A: 

for char* you need to create another variable that is long enough for both, for instance. You can 'fix' the length of the output string to remove the chance of overrunning the end of the string. If you do that, be careful to make this large enough to hold the whole number, otherwise you might find that book+50 and book+502 both come out as book+50 (truncation).

Here's how to manually calculate the amount of memory required. This is most efficient but error-prone.

int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);

ch2 now contains the combined text.

Alternatively, and slightly less tricky and also prettier (but less efficient) you can also do a 'trial run' of printf to get the required length:

int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; } 
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);

if your platform includes asprintf, then this is a lot easier, since asprintf automatically allocates the correct amount of memory for your new string.

int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);

ch2 now contains the combined text.

c++ is much less fiddly, but I'll leave that to others to describe.

Alex Brown
error C3861: 'snprintf': identifier not found.
aryan
#include <stdio.h> should fix that, but if it doesn't then there's something wrong with your build environment, or just possibly your platform doesn't include snprintf. This seems unlikely since it's one of the most important code security tools in existence.
Alex Brown
A: 
Try this out:
char *tmp  = new char [ stelen(original) ];
itoa(integer,intString,10);

output = strcat(tmp,intString);

//use output string 

delete [] tmp;
Ashish
this is not safe - since strcat simply writes more bytes to the end of the original string, and this may overwrite a nearby variable, causing your program to malfunction or crash. Also, the author didn't ask to modify the original string.
Alex Brown