Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

remove a token in c programming

Posted on 2015-02-10
7
Medium Priority
?
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

715 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