这是我的代码:
#include <stdio.h>
#include <string.h>
void ignoreRestOfLine(FILE* fp)
{
int c;
while ( (c = fgetc(fp)) != EOF && c != '\n');
}
int main( void )
{
int num_times, count =0;
int length;
scanf("%d ", &num_times);
char s[100];
for(count=0 ;count<num_times;count++){
if ( fgets(s, sizeof(s), stdin) == NULL )
{
// Deal with error.
}
if ( scanf("%d", &length) != 1 )
{
// Deal with error.
}
ignoreRestOfLine(stdin);
size_t n = strlen( s );
size_t m = 5;
int i,j;
for ( i = 0; i < strlen(s)+1; i++ ){
putchar( '[' );
for ( j = 0; j < m; j++ )
{
char c = s[(i + j) % ( n + 1 )];
if ( !c )
c = ' ';
putchar( c );
}
printf( "]\n" );
}
}
}
第一行是表示我要输入的符号数。第二行是我输入我希望我的符号输出的字符串的地方。第三行是选框内的空格数。例如:
Input:
1
Hello World!
5
Output:
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! ]
[d! H]
[! He]
[Hel ]
[ Hell]
但这就是我的代码实际所做的:
Input:
1
Hello World!
5
Output:
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld!
]
[ld!
]
[d!
H]
[!
He]
[
Hel]
[ Hell]
我该如何解决这个简单的错误?