Solved

remove a token in c programming

Posted on 2015-02-10
7
139 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
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.

708 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

19 Experts available now in Live!

Get 1:1 Help Now