Solved

remove a token in c programming

Posted on 2015-02-10
7
154 Views
Last Modified: 2015-02-11
This one is a little bit more complex than the last question :

char str[] ="<1.2.3.4:4747>;+instance=45;+gks=uuu.";

I want to remove one token only when it starts with +instance

i.e.

Entry:

"<1.2.3.4:4747>;+instance=45;+gks=uuu."

output:

"<1.2.3.4:4747>;+gks=uuu."

ideally I will do the removal on the buffer itself without copying to another buffer,
Is that possible ?
0
Comment
Question by:bachra04
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 40601982
strstr can be used to point to the start of the +instance string (returns, say pointer p0).
   http://www.cplusplus.com/reference/cstring/strstr/

Looks like the ; is your sentinel and you want to keep it.
I assume that ; is not part of the token you want to replace.
Then, you can use strchr to find the ; (returns, say pointer p1).
   http://www.cplusplus.com/reference/cstring/strchr/

strcpy( p0, p1) should copy the ; and beyond over the token starting at p0 - including the terminating null byte.
    http://www.cplusplus.com/reference/cstring/strcpy/
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40601984
By reviewing the error return values from strstr and strchr, you can add error checking to verify that you have the +instance string token, and the ; that you expect to follow.
0
 
LVL 86

Expert Comment

by:jkr
ID: 40601986
It's actually even easier than your last question, and yes, it can be done in the one buffer, e.g.

#include <string.h>

int remove_token(char* buf, const char* token, const char delim) {

    int idx = 0;
    char* delimpos = NULL;
    char* tokenpos = strstr(buf, token);

    if (!tokenpos) return -1; // token not found

    ixd = (int) tokenpos - buf;
    delimpos = strchr(buf + idx,delim);

    if (!delimpos) {
        buf[idx] = '\0';  // no following delimiter, cut of '+instance='

        return strlen(buf); // or just '1', as you like
    }
    
    strcpy(tokenpos, delimpos + 1); // copy the rest to the end of where the token was

    return strlen(buf); // or just '1', as you like
}

int main () {

    char str[] ="<1.2.3.4:4747>;+instance=45;+gks=uuu.";

    remove_token(str, "+instance", ';');
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 40602070
What I had in mind is essentially the same thing as jkr's code which I used as a starting point to keep the names the same and tweaked a little:
#include <string.h>
#include <stdio.h>

int remove_token(char* buf, const char* token, const char delim)
{
	char* delimpos = NULL;
	char* tokenpos = strstr(buf, token);

	if (!tokenpos) return -1; // token not found

	delimpos = strchr(tokenpos, delim);
	if (!delimpos) return -2; // delimiter not found

	strcpy(tokenpos, delimpos + 1); // copy the rest to the end of where the token was

	return 0;
}

int main () {

	char str[]  = "<1.2.3.4:4747>;+instance=45;+gks=uuu."; // good token
	char str2[] = "<1.2.3.4:4747>;+instance=45+gks=uuu.";  // left out delimiter

	printf("%s\n", str);
	if (remove_token(str, "+instance", ';') != 0)
	{
		printf("1) Error parsing token\n");
		return -98;
	}
	printf("%s\n", str);

	if (remove_token(str2, "+instance", ';') != 0)
	{
		printf("2) Error parsing token\n");
		return -99;
	}
	printf("%s\n", str2);
	return 0; // success
}

Open in new window

Output:
<1.2.3.4:4747>;+instance=45;+gks=uuu.
<1.2.3.4:4747>;+gks=uuu.
2) Error parsing token

Open in new window

0
 
LVL 2

Author Comment

by:bachra04
ID: 40602091
I think there is a bug when the input buffer has a long sentence with carriage return e,g.
<1.2.3.4:4747>;+instance=45;+gks=uuu.\r\n
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n
yyyyyyyyyyyyyyy\r\n

Etc ...

I m seeing some ovelap at the end , do you know the reason ?
0
 
LVL 2

Author Comment

by:bachra04
ID: 40602095
This is for JKr solution.  I will try phoffric solution as well.
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40602323
Should not make a difference in the end. Ichecked that and fixed one typo and one error and it works with your string, e.g.

#include <string.h>
#include <stdio.h>

int remove_token(char* buf, const char* token, const char delim) {

    int idx = 0;
    char* delimpos = NULL;
    char* tokenpos = strstr(buf, token);

    if (!tokenpos) return -1; // token not found

    idx = (int) (tokenpos - buf);
    delimpos = strchr(buf + idx,delim);

    if (!delimpos) {
        buf[idx] = '\0';  // no following delimiter, cut of '+instance='

        return strlen(buf); // or just '1', as you like
    }
    
    strcpy(tokenpos, delimpos + 1); // copy the rest to the end of where the token was

    return strlen(buf); // or just '1', as you like
}

int main () {

    char str[] ="<1.2.3.4:4747>;+instance=45;+gks=uuu.\r\n\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n\
yyyyyyyyyyyyyyy\r\n";

    remove_token(str, "+instance", ';');

    printf("%s\n", str);
}
                                          

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question