hi,
if I had:

char* arr;

and want to add character one by one into arr.
How could I? I don't wanna use vectors!

thanks

The same way you would add characters to a static array. The only difference being that you're responsible for allocating and deallocating the memory that 'arr' is going to point to.

there is a problem. imagine this:

char* arr = "foo bar"

now I wanna add character by character after the "foo bar";
I don't know how?
Maybe I know, but these days I'm a little oblivious :X

in php we can append string, how could I do this in C++:

$arr = "foo bar";
$arr .= " jar";
print $arr;

and result will be:

foo bar jar

>>in php we can append string, how could I do this in C++:
You could if you were using strings in C++. But the very fact is that you are not using strings but c-strings here.

#include<iostream>
#include<string>
int main()
{
   std::string arr="Foo Bar";
   std::cout<<arr; //prints Foo Bar
   arr+="X";
   std::cout<<arr;//prints Foo BarX
}

thanks, siddhant3s

Is there any way to do it without using std::string ?

I guess I can say yes.
If this won't hurt you:

char* arr="The Old String";
int old_len=strlen(arr);
char *old_ptr=arr;
arr=new char[old_len+1];
strcpy(arr,old_ptr);
strcat(arr, "X");
std::cout<<arr;//prints The Old StringX
//.
//.
//.
//after you used arr
delete[] arr;

Basically, I created another dynamic array and added a character.
[dit] I think it is obvious that you will have to #include <cstring>

thank you so much,
that is exactly what I need

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.