My task is to check the user input and replace each period with exclamation mark, and each exclamation mark with 2 exclamation marks, then count the number of substitutions made.
This is my code:
int main(void)
{
int userInput, substitutionsNum = 0;
printf("please enter your input:\n");
while ((userInput = getchar()) != '#')
{
if (userInput == '.')
{
userInput = '!';
++substitutionsNum;
}
else if (userInput == '!')
{
userInput = '!!';
++substitutionsNum;
}
}
printf("%c, the number of substitutions are: %d", userInput, substitutionsNum);
return 0;
}
If I put in the input “nir.” and then “#” to go out of the program, the output is “#, the number of substitutions are: 1”
You never print the input back out except once at the end, so the “replacement” won’t work.
Also, you can’t represent a pair of exclamation points as
'!!', that’s a multi-character literal which is not the same. At least, no I/O functions will do what you expect with it, if you try to print it for instance.