Solved

remove a token in c programming

Posted on 2015-02-10
7
158 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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