Sign in to follow this  

Editing strings

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a few commands for my console app when I ran into the problem about editing the command to do specific functions on user input. For example, I have a "-say" and a "-do" command. the say command would make the console output whatever string I input after "-say"(for example, "-say Hello World!"). The problem I ran into is stripping the string if the command whenever it's needed. One solution I tried was using a normal string (char str[];) then doing str += 5; to remove the first 5 characters. That way doesn't work all the time though, so if i typed in the console, -shu hello world!, the command would still think I said -say hello world. In general, what I need is a way to remove a string from the input(the string being "-say " or "-do "(with the space included)). Thanks

Share this post


Link to post
Share on other sites
So, I understand you are using C.

Step 2: remove the first 5 characters and display (you already did that).
Step 1: check that the four first characters are '-', 's', 'a' and 'y'.

Share this post


Link to post
Share on other sites
Quote:
In general, what I need is a way to remove a string from the input(the string being "-say " or "-do "(with the space included)).


Is the string always at the start of the string, also is it guarenteed that the string is only one time in the input?

If it's always at the start you can do something like this (basically what ToohrVyk said, just in a function):

bool is_equal_fixed(char* str1,char* str2,size_t len)
{
assert( strlen(str1) >= len );
assert( strlen(str2) >= len );

for( int i = 0;i<len;++i)
{
if( str1[i] != str2[i] )
return false;
}
return true;
}

bool starts_with(char* input,char* command)
{
// is size_t a C type?
size_t cmd_length = strlen(command);

// Make sure input is longer than the command.
assert(cmd_length <= strlen(input) );

return is_equal_fixed(input,command,cmd_length);
}



EDIT: If I understand your description right, then you do something like the following:

char str[255]; // Input cant be longer than 255
get_input(str);
char* out = 0;
if( starts_with_say(str ) )
{
out = (str+5);
}


If that is basically what you are doing then you can run into a problem if str is deleted before we are done using out.

Share this post


Link to post
Share on other sites
This is a simple proble with a really simple solution, lets not try to over complicate it fellas.

This works just fine

if (str == strstr(str, "-say"))
{
printf("%s\n", (str+5));
}

Share this post


Link to post
Share on other sites
your absolutely right. if we disregard the trailing space, we run the possiblity of the user submitting just "-say" and running *("-say"+5), will leave us with a pointer directly after the end of the buffer passed the null-byte even, so who knows what this would return.

The correct code would be

if (str == strstr(str, "-say "))
{
printf("%s\n", (str+5));
}


Share this post


Link to post
Share on other sites

This topic is 4305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this