I am working on expand function, that asks to write a C++ command-line program that includes a main function and an expand function. The main function should ask the user to input a string and a character, output the original string, call the expand function, then output the expanded string. Funciton expand should not contain any cin or cout statements.
The signature of expand is
void expand(char str[], char mark);
Before expansion: banana
After expansion: ba na na
(My issue is it print out:
Before expansion: banana
(space)After expansion: banan )
One thing I see is that you are using sizeof() to get the size of a pointer to char. That gives you the size of the pointer, not the C-string. Use strlen() instead.
And what is expand supposed to do? Put a space after every character "mark" in the given string?
How are you supposed to modify the C-string you are passing in? Re-allocate memory or something? Or is the function only suppose to print the expanded string?
The function is suppose to print the expanded string, re-allocate memory is only my thought of solving this problem, so it may not be right at all. But it seems right to me in some sense, but when I run the program after fixing the strlen(), it prints out some spaces in the very beginning of the line like:
Before expansion: banana
After expansion: banana
instead of wanted:
Before expansion: banana
After expansion: ba na na