Shoutbox

Copying part of a string - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Copying part of a string (/showthread.php?tid=97600)

Copying part of a string by Chancer on 05-14-2011 at 08:46 PM

I'm making a (rudimentary) chat system at college (server + client), and it works like this:

The user (using the client app) sends a string in the format COMMAND:text, ie, "USER:Chancer" changes my name to Chancer.

The problem is, it's one single string, and I need to read the text parameter alone.
I'm doing this:

c code:
for (k = 5; buffer[k - 1] != '\0'; k++)
    text[k - 5] = buffer[k];
buffer is the string sent by the user, k starts at 5 because all available commands are in the 4-char - colon pattern.
It's partially working, but I'm afraid the text vector is not receiving the '\0' character.

Previoulsy I had this, and it was OK:
c code:
for (k = 5; buffer[k] != '\0'; k++)
    text[k - 5] = buffer[k];
text[k - 6] = '\0';

But I just think it doesn't look good. I know that using "%.5s", for example, I can print 5 characters of a string. Is there anyway to print "from character 5 until the end (\0)"?

Edit:
Cool. I found a command "sscanf" which can split a string in many different strings in any pattern you choose.
RE: Copying part of a string by Chancer on 05-15-2011 at 05:10 PM

Damn... sscanf is not working as I imagined. I can get the command string, but only the first word of the parameter, if it has spaces.

I also tried

code:
sprintf (msg, "%s", &buffer[5]);
but it also has problems with the \0

Ideas?
RE: Copying part of a string by Adeptus on 06-08-2011 at 05:49 AM

You probably have solved this by now, but a standard C library function you might want to take a look at, even if you did it another, way is strtok().