Strtok Processing String Constants

Source: Internet
Author: User
Tags printf strtok

The Strtok () function cannot handle the const char* directly, and a char array is obtained with strncpy, and the temp array is processed.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
int main (void)
{
    Char *string = "t01:91+:123";
    Char tmp[16];
    char* s;
 
    /* Strtok can does with const char*, so I use the strncpy copy it to an char array *
    /strncpy (tmp, String, sizeof (string ));
 
    s = strtok (tmp, "T");
    printf ("String:%s\n", string); 01:91+:123
    s = strtok (S, ":");
 
    printf ("City ID:%s\n", s);
    return 0;
}

----------

Gist:https://gist.github.com/wusuopubupt/64cd0cb29bf67ed82af4


Update:

------------

#include <string.h>
#include <stdio.h>
int main (void)
{
    char input[16]= "abc,d";
    char *p;
    P=strtok (Input, ",");
    if (p) printf ("%s\n", p);

    P=strtok (NULL, ","); Attention here.
    if (p) printf ("%s\n", p);
    return 0;
}

Output:

Abc

D

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.