I am compressing string. And the compressed string sometimes having NULL character inside before the end NULL. I want to return the string till the end null.But the compressor function is returning the sting till the occurring of the first NULL. Please help me.
char* compressor(char* str)
{
char *compressed_string;
//After some calculation
compressed_string="bk`NULL`dk";// at the last here is automatic an NULL we all know
return compressed_string;
}
void main()
{
char* str;
str=compressor("Muhammad Ashikuzzaman");
printf("Compressed Value = %s",str);
}
The output is : Compressed Value = bk; And all other characters from compressor function is not here. Is there any way to show all the string.
Solution using
std::string
:Note the usage of std::string, as well as how the information is outputted using the
copy
algorithm function. The reason whycopy
is used instead ofprintf
is to ensure that all of the characters, including the (invisible) embedded NULL's are printed.Also, the size of the compressed data is easily retrieved by calling
str::size()
.The function returns "entire string". It is printf that outputs it until the null character will be encountered.
You could define the function the following way
To understand the problem consider the following code
The output is