Solved

remove a token in c programming

Posted on 2015-02-10
7
146 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 411
Which IDE to use to begin C++ training? 5 59
mixing C++ & C# in Vis Studio 2013 7 141
How to gracefully close the c++ 11 thread? 3 83
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now