我已經在 C 中運行了這段代碼,
char *bracket_by_len(char *output, char *word, int length)
{
if(strlen(word) < 5) {
output[0] = '<';
output[1] = '<';
/*int i = 2;
while(i != (strlen(word) 2)) {
output[i] = word[i - 2];
i = 1;
}*/
strncat(output, word, strlen(word));
strcat(output, ">>");
return output;
} else {... not using them as 4 letter word for input}
int main()
{
char word[40], output[40]; // word is not used.
printf("%s \n.", bracket_by_len(output, "word", 20);
return 0;
}
實際代碼是這樣的:輸入代碼。 然后我寫了
return output;
我列印了這個
printf("Based on size, \t %s.\n", output);
輸出如下所示:
Based on size, <<^S>>.
開頭有一些隨機字符。當我用手動復制單詞字母的 while 回圈替換 strncat() 函式時,輸入沒問題。
提前致謝。
uj5u.com熱心網友回復:
output尚未以 NULL 終止。在左側 V 形后添加它。
output[0] = '<';
output[1] = '<';
output[2] = 0;
strncat(output, word, strlen(word));
printf("%s\n", output);
如果您sprintf改為使用,則不必擔心添加 null 那個細節。
sprintf(output, "<<");
strncat(output, word, strlen(word));
printf("%s\n", output);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331931.html
