I wrote the following C++ program to capitalize the first letter of each word in a sentence entered by the user:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Intstr[255];
char Outstr[255];
fgets(Intstr,255,stdin);
int i = 0;
Outstr[0] = toupper(Intstr[0]);
for (i = 1; Intstr[i]; i++)
{
if (Intstr[i-1]==' ')
{
Outstr[i]=toupper(Intstr[i]);
}
else
{
Outstr[i]=Intstr[i];
}
}
cout << Outstr << endl;
return 0;
}
When I entered "hello world" into the command line, the expected output is "Hello World". However, the actual output is
Hello World╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠hello world
How can I fix this code so that the output is "Hello World"?