c++ - How to add a question mark to the end of a line? -
i want check see if user added ?
end of buffer. if not, want program add 1 automatically. have far. dont know next.
first check see if buffer not blank.
then, if last item not ?
, add question mark automatically buffer , copy content current data node.
if ( strlen(buffer) != 0) { if (buffer[strlen(buffer)-1] != '?') { //what put here add ? if theres non? } strcpy(current->data,buffer); }
from can see, don't gain modifying buffer
in way. can add ?
current->data
if needed.
int len = strlen(buffer); strcpy(current->data, buffer); if (len && buffer[len-1] != '?') { current->data[len] = '?'; current->data[len+1] = '\0'; }
if option, should consider changing code use std::string
instead.
std::string buffer = input(); if (!buffer.empty() && buffer.back() != '?') buffer += '?'; std::copy(buffer.begin(), buffer.end(), current->data); current->data[buffer.size()] = '\0';
if don't have c++11 compiler, use *buffer.rbegin()
instead of buffer.back()
.
Comments
Post a Comment