Remove string characters

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

Recommended Posts

Ok so basically, I have a winsock program(that's not the point though, this isn't a network question) that has a master client which sends messages and all the regular clients who just recieve messages. The first command I am trying to put into my server/client is execution, like the windows cmd. The master client sends a message that starts with "start", an example would be "start notepad.exe". What I need to do is strip the string of the characters "start "(including the space), so that the only characters left would be notepad.exe. This is what I got so far, didn't work but i'm pretty sure I am using the wrong method to erase characters
                                      for (int i = 0; i < 6; i++) START[i] = ' '; ShellExecute(NULL, "open", message, NULL, NULL, SW_SHOW);


EDIT: I have to use a char(instead of string) because the message is used for sending bytes over a network aswell EDIT2: I have also tried using message.erase() but i get this error: request for member erase' in massage', which is of non-class type char[1024]' [Edited by - cherryhouse on January 28, 2006 8:54:42 PM]

Share on other sites
Is this so people can do "format C:" remotely on each others machines?

Share on other sites
It's not for any in particular reason. I am just trying to pack my program with as many capabilities as I can for experience. This problem I am having is a good example, I didn't know how to remove characters and hopefully I will know how to before I move on to the next function of the program. If i wanted to do a format I would just use SHFormatDrive(). If you feel I am going to use this for dangerous purposes, but know the answer then don't give me the answer :)

Not to mention an easier way to go about doing this would be

char start[7] = "start ";strcat(start, message);system(start);

That way doesn't teach me anything though.

Share on other sites
This is a part of my program for text entry that utilizes character arrays. I think this applies to what you need.

//~~~~~Text Input Structurestruct sTextInput{    float fXPos;    float fYPos;    float fCursorX;    float fCursorY;    char cInput[64];};extern sTextInput TextInput;...            if(strlen(TextInput.cInput) > 0)            {                char cTempBuffer[64];                strcpy(cTempBuffer, TextInput.cInput);                strcpy(TextInput.cInput, "");                for(int loop = 0; loop < strlen(cTempBuffer) - 1; loop++)                {                    char cTempBuffer_2[2];                    sprintf(cTempBuffer_2, "%c", cTempBuffer[loop]);                    strcat(TextInput.cInput, cTempBuffer_2);                }                //strncpy(TextInput.cInput, cTempBuffer, 2);                //strncpy(cTempBuffer, TextInput.cInput, 2);                strcat(TextInput.cInput, "\0");            }

Simply put, create a new character array equal or more than your initial, copy over the contents one character at a time with the exception of the last one or whatever the length, and append a null terminator. In your case, just copy over the length of what you want to keep and append whatever you need at the end.

Share on other sites
If you just want to do it for this specific thing then you can do it easy.

If you know how many characters you want trim off begining for example, you can use strncpy, and when it asks you for source you specify "your string + offset".

Or if you want easy way in this case you can do something like:

char * pszString = "start Notepad.exe";while(* pszString ++ != ' ');//pszString now is "Notepad.exe"`

Will trim front of string untill it see's a space in this case right after start, leaving pszString with "Notepad.exe".

Hope it helps.

P.S.
Of course this is only if you know that your words are seperated by 1 space, and there is even a space... so make checks, if not then you are not sure how many spaces you can write a function similar way as above to trim it.

Share on other sites

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628686
• Total Posts
2984234

• 14
• 13
• 13
• 10
• 10