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

How to get back a signed string (e.g. md5 hash) using a public-key (x509 certificate)

python m2crypto

I'm very new with cryptographic and stuck for 2 days with this problem, I hope you can help me with.

How get query with parameters from ODBC on C#

c# sql parameters odbc

how can i print a query parsed from ODBC on C# after the binding of parameters?

Perl - How create method alias in child class?

perl class

I'm trying to create an alias to a super class generic method in the child class, but it doesn't seem to work. This trick inside the same class works, but it doesn't seem to work across the super/...

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.