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 synthesize piano sounds in android/java

java android audio synthesis

I have made a few simple apps on android, and thought it was time for something a bit more complex. So, i thought I'd try something that's already out there, but build it from scratch.

How to avoid to do 100 queries in this case?

php mysql

I have created a simple blog in php/mysql that show in homepage latest 100 posts and for every post show the comments's number.

Using . bks keystores certificate in iPhone app

iphone objective-c ios bouncycastle

I am building an iPhone app that needs .bks keystores to authenticate. I did not find anything about this information for iOS apps .