[英]String manipulating, What does the following function do?
試圖弄清楚下一個功能的目的是什么?
我知道它正在對字符串進行一些操作-獲取char指針-命令,檢查是否有空間或制表符空間...但是最后我不明白該函數在做什么?
void FixCommand(char* command)
{
char newCommand[MAX_COMMAND_SIZE + 1];
char* currChar = command;
int lastConfirmed = 0;
int inputIndex = 0;
while ((*currChar == ' ') || (*currChar == '\t'))
{
++currChar;
}
while (*currChar != 0)
{
if (*currChar != '\n')
{
newCommand[inputIndex] = *currChar;
++inputIndex;
if ((*currChar != ' ') && (*currChar != '\t'))
{
lastConfirmed = inputIndex;
}
}
++currChar;
}
newCommand[lastConfirmed] = 0;
strcpy(command, newCommand);
}
該程序跳過command
字符串中的初始空格/制表符,然后將所有字符復制到newCommand[]
,跳過“換行” \\n
字符。 它還在command
字符串中查找換行符(空格或制表符),記下它看到的最后一個非空白字符,並標記其位置。 最后,將command
從第一個非空白到最后一個非空白的部分復制回command
,並刪除\\n
字符。
例如,如果傳入的命令字符串如下所示:
" quick brown\nfox\tjumps over the\tlazy dog\t "
那么輸出將如下所示:
"quick brownfox\tjumps over the\tlazy dog"
其目的可能是在將命令字符串傳遞到不允許\\n
和前導/結尾空格的外部系統之前對其進行“清理”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.