How to add a question mark to the end of a line?

I want to check to see if the user added a ? to the end of the buffer. If not, I want the program to add one automatically. This is what I have so far. I dont know what to do next.

First I check to see if the buffer is not blank. Then, if the last item is not a ?, add the question mark automatically to the buffer and then copy the content to the current data node.

if ( strlen(buffer) != 0)
{
   if (buffer[strlen(buffer)-1] != '?')
   {

           //what do i put here to add the ? if theres non?    
   }

strcpy(current->data,buffer);

}

Answers


From what I can see, you don't gain anything from modifying buffer in this way. You can simply add the ? to current->data if it is needed.

int len = strlen(buffer);
strcpy(current->data, buffer);
if (len && buffer[len-1] != '?') {
    current->data[len] = '?';
    current->data[len+1] = '\0';
}

If it is an option, you should consider changing your code to 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 you don't have a C++11 compiler, use *buffer.rbegin() instead of buffer.back().


Need Your Help

Issue arranging new leaves in MultiSpliPane (with SSCCE)

java swing jtable swingx jxmultisplitpane

I am creating an app which adds Tables to a Form and arrages them in a specific layout. I'm creating a new leaf in MultiSplitPane everytime a new table needs to be added to the form, and I'm adding...

Unable to attach <p:ajax> to non-ClientBehaviorHolder parent

java html ajax primefaces drop-down-menu

Im trying to call a function on selection of a value from dropdown. Heres my code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.