Member Avatar for Member #808376

guys how to slpit a date string ie....30/jully/2001

i want the following output
30
jully 2001


please help...

Dani AI

Generated

A couple of compact, safe ways to split a string like 30/jully/2001 into the day (30) and the remainder (jully 2001). was right that tokenizing is simple; 's manual-parse approach is valid too; 's spit() was a typo (as pointed out). Below are two practical, tested patterns and a few cautions.

Use scanf-style scansets to parse without modifying the original string and to avoid buffer overruns:

#include <stdio.h>

int main(void) {
    const char *s = "30/jully/2001";
    char day[3] = {0}, month[20] = {0}, year[10] = {0};

    if (sscanf(s, "%2[^/]/%19[^/]/%9[^/]", day, month, year) == 3) {
        printf("%s\n%s %s\n", day, month, year);
    } else {
        fprintf(stderr, "invalid date format\n");
    }
    return 0;
}

Or, if you prefer tokenizing (as suggested by ), strtok is straightforward but it modifies the buffer and is not thread-safe; use strtok_r on POSIX if you need reentrancy:

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

int main(void) {
    char buf[] = "30/jully/2001";
    char *p = strtok(buf, "/");
    if (p) {
        printf("%s\n", p);
        p = strtok(NULL, "/");
        if (p) {
            printf("%s ", p);
            p = strtok(NULL, "/");
            if (p) printf("%s\n", p);
        }
    }
    return 0;
}

Quick tips: always check return values, size your buffers with a margin, trim whitespace, and validate delimiters. If you need robust, locale-aware date parsing (different formats, numeric months, or real date validation), use a proper date parser or strptime on POSIX systems — otherwise normalize and validate the month string yourself (and watch for typos like "jully").

Recommended Answers

All 6 Replies

We only give homework help to those who show effort.

Look up the strtok function.

guys how to slpit a date string ie....30/jully/2001

  1. Read the date into a char array (from the user or a file or wherever).
  2. A date can have a simple and strict format, so you can specify a few acceptable ones and compare the input to those.
  3. Go through the array one character at a time and copy the respective parts to new arrays for the day, month and year.
  4. Done. output the result.

Have a go at doing something like that and then see how you get on. If you have further problems, then post the code that you've written and explain what errors you have in a bit more detail.

we can use spit() which can be used to split a char array using a delimiter

we can use spit()

In what library? That's not in the Standard C library for sure.

sorry
then its better to use strtok as said by narue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.