what is wrong with this print_repeat function?
print_repeat should print out the string s, but repeat the ith character i times (starting counting at 1).
So print_repeat("this") should print thhiiissssint print_repeat(char s[]){
int i,j;
i = 0;
while (s[i] != '\0');{
for (j = 1; j <= i+1; j+1){
putchar(s[i]);
}
}
开发者_如何学编程return 0;
}
void main()
{
print_repeat("this");
}
Find the correct code below:
while (s[i] != '\0')
{
for (j = 1; j <= i+1; j++)
{
putchar(s[i]);
}
i++;
}
The main problems were: 1. Not incrementing the variable i inside while loop 2. Variable j is not incremented correctly 3. There is semicolon right after the while which is incorrect.
Suggestion: try debugging your code. That will help you learn faster.
In your print_repeat
code, you never increment i
in your while
loop, which would result in infinite loops.
add "i++;" on the end of the while loop.
精彩评论