[英]How to add a question mark to the end of a line?
我想檢查用戶是否添加了?
到緩沖區的末尾。 如果沒有,我希望程序自動添加一個。 到目前為止,這就是我所擁有的。 我不知道下一步該怎么做。
首先,我檢查緩沖區是否為空。
然后,如果最后一項不是?
,將問號自動添加到緩沖區中,然后將內容復制到當前數據節點。
if ( strlen(buffer) != 0)
{
if (buffer[strlen(buffer)-1] != '?')
{
//what do i put here to add the ? if theres non?
}
strcpy(current->data,buffer);
}
據我所知,以這種方式修改buffer
沒有任何好處。 您可以簡單地添加?
到當前- current->data
如果需要)。
int len = strlen(buffer);
strcpy(current->data, buffer);
if (len && buffer[len-1] != '?') {
current->data[len] = '?';
current->data[len+1] = '\0';
}
如果可以選擇,則應考慮更改代碼以改為使用std::string
。
std::string buffer = input();
if (!buffer.empty() && buffer.back() != '?') buffer += '?';
std::copy(buffer.begin(), buffer.end(), current->data);
current->data[buffer.size()] = '\0';
如果您沒有C ++ 11編譯器,請使用*buffer.rbegin()
而不是buffer.back()
。
您需要將字符串與用戶正在寫的帶有問號one的消息進行串聯。為此,您可以使用concatenate方法。本文介紹了此方法。 串聯字符串無法正常工作 。
在連接問號之前,為什么不創建一個檢查最后一個字符是否為問號的函數?
//Create function that returns a bool
bool isQuestionMark(char * buffer)
{
//Create pointer to buffer
char * pointer = buffer;
//Go to the null character
while(*pointer != '\0')
pointer++;
//Get to the last character
pointer--;
//Check to see if last character is a question mark
if(*pointer == '?')
return true;
else
return false;
}
然后,您想調用該函數以查看是否需要連接問號。
if(isQuestionMark(buffer) == true)
strcat(buffer, "?");
else
//Do nothing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.