:
: How to initialize a string.. so that it can hold any size..
:
: char str[100] i dont want to do this way...str[] should be able to
: support as many characters as input by user..
:
depends -- is this a c or c++ program? in c++ you can use std::string class which expands as needed. In C you have to use pointer and initialially malloc() it to fixed length, then expand it as needed using realloc(). Don't normally have to be concerned if the string is a little larger than required -- to speed your program up a tiny bit don't call realloc() to allocate just a single character, but call it to allocate a block of characters. For example:
char *buffer = 0;
long bufsize = 0;
const int BLOCKSIZE 36;
char key;
int keycount = 0;
while( (key = getchar()) != EOF)
{
if( (keycount+1) >= bufsize)
{
bufsize += BLOCKSIZE;
buffer = realloc(bufsize);
}
buffer[keycount] = key;
keycount++;
}
=============================================
never lie -- the government doesn't like the competition. (Author unknown)