So, let us first analyze your program. Here are the findings:
- You are using a mixture of C++ and C. You should decide for one language. You tagged the question with C++
- Include file is missing
- In my opinion you are still on C. But unfortunately, also this with bugs
using namespace std;
shoulb be avoided. You can read many many posts here on SO about the why
- Plain C-Style array like
char a[100];
should not be used in C++. They are very error prone
- For strings you should use
std::string
in C++
- In you for loop you are mixing signed and unsigned values.
std::strlen
returns the unsigned type std::size_t
strcat
should not be used. My compiler does not even compile it.
strcat
with plain C-Style arrays is very dangerous and will often lead to undefined behaviour
- The compiler already tells you one problem.
strcat
expects a char *
as first element. You are giving it a char
strcat
does not do what you expect. It always appends the 2nd parameter at the end of the string. Not on a certain position
- So, even if you correct it to
strcat(&a[i], "test ");
, "test " will not be inserted at position i, but at the end of the string. Always leading to a desaster, if you enter a string with a length of 100 and spaces near the end.
- There is not a single line of comment in the example
- Variable names like 'a' have no meaning. Always use meaningful variable names
- You are using
i++
in your for loop. Always use ++i
- Magic numbers like "100" should be avoided. Why 100? Whay not 99, 101 or 555?
- Function
main
has been defined as int
. It should return a value, e.g.return 0;
So, quite some problems in your code
We have the following categories of problems:
- Hard syntax errors
- Design errors
- Semantic errors
- Style errors
How to fix? Since we are still rather on C, experts like David should give the answer, but I will try to do my best. 2 Solutions:
- Continue with C-Style
- Write a C++ program
So, one of many possible examples for a C-Style solution
#include <iostream>
#include <cstring>
int main()
{
// We want to work on a string with the maximum length of 0, including the terminating 0
constexpr size_t MaxStringSize = 10U;
// Here we will store the string in a C-Sytle char array
char myText[MaxStringSize];
// Now get the string from the user. Limit to a maximum length
std::cin.get(myText, MaxStringSize);
// We want to replace a space with the following text
const char* replaceText = "test ";
const size_t replaceTextLength = std::strlen(replaceText);
// Now, iterate over all characters in the string given by the user and replace spaces
for (size_t i = 0U; i < std::strlen(myText); ++i) {
// Check, if the current character is a space, because then we need to replace
if (myText[i] == ' ')
{
// We need space for the replacement text. So, we shift all characters at position i to the right
// Caveats. The string array has a maximum length. We must not shift over the border
if ((i + replaceTextLength +strlen(&myText[i])) < MaxStringSize) {
// make free space
std::memmove(&myText[i]+ replaceTextLength, &myText[i]+1, strlen(&myText[i])+1);
// Copy replacement text
std::memmove(&myText[i], replaceText, replaceTextLength);
}
}
}
// Show result to the user
std::cout << myText;
return 0;
}
And now the C++ solution. Using the C++ standard library.
#include <iostream>
#include <string>
#include <regex>
int main() {
// Read a string from the user and check, if that operatrion was succesful
if (std::string line{}; std::getline(std::cin, line)) {
// Show text with replacements to the user
std::cout << std::regex_replace(line, std::regex(" "), "test ") << "\n";
}
return 0;
}