Reputation: 345
I am making a c++ program which will replace all the spaces from a char array with "%20". For ex, "This is some text with some spaces" will be converted to "This%20is%20some%20text%20with%20some%20spaces"
PLEASE SEE IN PROGRAM COMMENTS WHICH LINE IS NOT WORKING PROPERLY.
replacer function in this program takes pointer to char array and size of the string. Then method creates new char array with replaced spaces and function returns pointer to replaced char array.
The problem in this program is with printing char array in main function whose pointer is returned by the replacer.
"replaced" is the pointer variable which is pointing to replaced string. In main function I am trying to print char array using "replaced" pointer.
If I print everything manually it works fine.
Eg, if i do : cout << *replaced << *(replaced+1) << *(replaced+2)
....
The whole string will be printed.
But inside loop it gives me garbage value. Please let me know where in the code I am making any mistake. Thank you.
#include <iostream>
using namespace std;
char * replacer(char * text, unsigned int size )
{
int space_count = 0, i = 0, new_size = 0;
cout << "Given text : ";
for(i = 0;i < size;i++)
{
cout << *(text + i) ;
char temp = *(text + i);
if (temp == ' ')
{
space_count++;
}
}
cout << endl;
cout << "Number of spaces in given text : " << space_count << endl;
new_size = size + ( 2 * space_count );
char replaced_string[new_size];
int counter = 0 ;
for(i = 0 ;i < size; i++)
{
char temp = *(text + i);
if(temp == ' ')
{
replaced_string[counter++] = '%';
replaced_string[counter++] = '2';
replaced_string[counter++] = '0';
}
else
{
replaced_string[counter++] = temp;
}
}
cout << "in replacer method : " << replaced_string << endl;
char * pointer_to_replaced;
pointer_to_replaced = replaced_string;
return pointer_to_replaced;
}
int main()
{
int t;
char original[] = "This is some text with some spaces";
char * original_pointer = original;
char *replaced;
replaced = replacer(original_pointer, sizeof(original));
//cout << *(replaced) << *(replaced+1) << *(replaced+2) << *(replaced+3) << *(replaced+4) << endl;
// ABOVE LINE WORKS FINE
for(t=0;t<20;t++)
{
cout<<*(replaced+t); // THIS IS NOT WORKING PROPERLY
}
cout <<endl;
return 0;
}
Upvotes: 0
Views: 432
Reputation: 75545
The first problem is that you are allocating replaced_string
on the stack, and then expecting it to retain its value after returning from the function. You should allocate on the heap instead, and remember to delete[]
the memory afterwards. The stack goes away when a function returns.
// char replaced_string[new_size];
char* replaced_string = new char[new_size];
A second problem is that you need to null-terminate your replaced_string
after the loop.
replaced_string[counter] = '\0';
Additionally, you do not need to loop through to print out the return value. You can use cout
directly.
cout << replaced << endl;
There may be other latent issues as well, but fixing those three will make the code functional.
Upvotes: 3