Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

c - How to extract the string if we have have more than one delimiters?

If I have this code:

int main ()
{
char *sentence = "Wisteria#Tunnel";
char stringA[50];
char stringB[50];
char stringC[50];
pDelim = strstr(sentence,"#");
*pDelim = '';
strcpy(stringA,sentence);
strcpy(stringB,(pDelim+1));
    return(0);
}

After running it, stringA will be "Wisteria" and stringB will be "Tunnel", right?

If :

char *sentence = "Wisteria#Tunnel#Japan";

How to extracted it to three part , stringA will be "Wisteria" , stringB will be "Tunnel" and stringC will be "Japan"

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Standard string functions strtok, strstr, strchr all will work, provided the surrounding code is correct as well. That is, if you want to search multiple times in the same source string, you need to point a Start-at pointer at the start of your source, and after each (succesful!) search update it to point after your delimiter. By default, strtok will work fine for your purpose -- a single character delimiter. You can read about strtok in any good reference (I'm not going to look it up for you).

In fact it is such a simple operation, I made this up just now. Other than vanilla strtok, my strcpyUpTo accepts any delimiter string. I could have used strstr to first check, but I prefer copying right away. The strncmp and strlen inside the copy loop may be inefficient; then again, in general these low-level string operations should be pretty fast nevertheless.

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

char *strcpyUpTo (const char *source, char *dest, const char *delim)
{
    while (*source && strncmp (source, delim, strlen(delim)))
        *dest++ = *source++;
    *dest = 0;

    if (*source)
        return (char *)(source+strlen(delim));
    return (char *)source;
}

int main (void)
{
    char *sentence = "Wisteria#any#Tunnel#any#Japan";
    char stringA[50];
    char stringB[50];
    char stringC[50];
    char *pDelim;

    pDelim = strcpyUpTo (sentence, stringA, "#any#");
    pDelim = strcpyUpTo (pDelim, stringB, "#any#");
    pDelim = strcpyUpTo (pDelim, stringC, "#any#");

    printf ("stringA = "%s"
", stringA);
    printf ("stringB = "%s"
", stringB);
    printf ("stringC = "%s"
", stringC);

    return 0;
}

Addition

For an unknown number of substrings, you can use a while loop such as the following. (For production code, it needs all kinds of checks. Left out for brevity, for clarity, and as exercise for the reader.) It's basically how you'd use strtok as well.

char resultList[10][50];
int i, n_result;

pDelim = sentence;
n_result = 0;
do
{
    pDelim = strcpyUpTo (pDelim, resultList[n_result], "#any#");
    n_result++;
} while (*pDelim && n_result < 10);

printf ("number of strings: %d
", n_result);
for (i=0; i<n_result; i++)
    printf ("string %d = "%s"
", i, resultList[i]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...