Como gerar uma string de letreiro menor que o tamanho do sinal de letreiro?
Portanto, meu código recebe uma sequência de letras e, em seguida, gera essa sequência com o tamanho do sinal sendo 5. Portanto, por exemplo, se eu quiser exibir "Hello World!", A saída é:
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
Mas o problema é que, se houver uma sequência de letras com comprimento 8 e o tamanho do letreiro for 10, desejo exibir a sequência apenas uma vez no letreiro. Portanto, se uma string tiver um tamanho menor que o sinal de letreiro indicado, apenas exiba a string uma vez. Por exemplo:
Input:
Activist (the string that I want to output in a sign)
10 (the length of the sign)
Output:
[Activist ]
Observe como ainda existem 10 espaços no letreiro e tudo o que faz é simplesmente emitir a string sozinha uma vez.
Aqui está o meu código. Eu fiz para executar mais de uma vez, se indicado:
#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 marq_length, sign = 0;
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", &marq_length) != 1) {
// Deal with error.
}
ignoreRestOfLine(stdin);
size_t n = strlen(s) - 1;
int i, j;
if (s[strlen(s) - 1] == '\n')
s[strlen(s) - 1] = '\0';
printf("Sign #%d:\n", ++sign);
for (i = 0; i < n + 1; i++) {
putchar('[');
for (j = 0; j < marq_length; j++) {
char c = s[(i + j) % (n + 1)];
if (!c)
c = ' ';
putchar(c);
}
printf( "]\n" );
}
}
}
A entrada e saída para isso é a seguinte:
Input:
3
Hello World!
5
Sign #1: (This is the output)
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
Activist
10
Sign #2: (This is the output)
[Activist A]
[ctivist Ac]
[tivist Act]
[ivist Acti]
[vist Activ]
[ist Activi]
[st Activis]
[t Activist]
[ Activist ]
LOL
2
Sign #3: (This is the output)
[LO]
[OL]
[L ]
[ L]
Está tudo certo, exceto pelo sinal # 2. Como eu produzo apenas a string uma vez no sinal de letreiro se o comprimento da string é menor que o tamanho do letreiro?