manipulate string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crovax75@hotmail.com

    #1

    manipulate string

    LS,

    I've a problem to get the year, month and day from the following
    string: "20051027"

    --

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

    char str[9]="20051027";

    char year[5], month[3], day[3];




    int main(void){

    printf("String str: %s\n",str);


    strncpy(year, str, 4);
    year[4] = '\0';
    printf("%s\n", year);


    //But how to get the month in the string 'str'?


    printf("Year:%s \n",year);
    printf("Month:% s\n",month);
    printf("Day:%s\ n",day);

    getchar();
    return 0;
    }

    --

    PS. It is not a problem to concert it to integers.

    Greetz,

    Marcel

  • Christopher Benson-Manica

    #2
    Re: manipulate string

    crovax75@hotmai l.com wrote:
    [color=blue]
    > #include <stdio.h>
    > #include <string.h>[/color]
    [color=blue]
    > char str[9]="20051027";[/color]
    [color=blue]
    > char year[5], month[3], day[3];[/color]
    [color=blue]
    > int main(void){[/color]
    [color=blue]
    > strncpy(year, str, 4);[/color]

    If you're feeling adventurous, you could use sscanf() for this task.
    The following is easier, though...

    sprintf( year, "%.4s", str );
    sprintf( month, "%.2s", str+4 );
    sprintf( day, "%.2s", str+6 );

    assuming, of course, that you have somehow verified that str is in the
    format you expect.
    [color=blue]
    > year[4] = '\0';
    > printf("%s\n", year);[/color]

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Jirka Klaue

      #3
      Re: manipulate string

      Christopher Benson-Manica:[color=blue]
      > crovax75@hotmai l.com:
      >[color=green]
      >> char str[9]="20051027";[/color][/color]
      ....[color=blue]
      > If you're feeling adventurous, you could use sscanf() for this task.[/color]

      IMO sscanf is *the* tool for this task.

      unsigned y, m, d; /* if you insist on strings, do the obvious */

      if (3 == sscanf(str, "%4u%2u%2u" , &y, &m, &d))
      printf("%u %u %u\n", y, m, d);
      [color=blue]
      > The following is easier, though...
      >
      > sprintf( year, "%.4s", str );
      > sprintf( month, "%.2s", str+4 );
      > sprintf( day, "%.2s", str+6 );[/color]

      Well, longer. More error-prone. But easier?
      [color=blue]
      > assuming, of course, that you have somehow verified that str is in the
      > format you expect.[/color]

      OK, sscanf can do this for you.

      Jirka

      Comment

      • Gow

        #4
        Re: manipulate string

        Christopher Benson-Manica wrote:[color=blue]
        > crovax75@hotmai l.com wrote:
        >[color=green]
        > > #include <stdio.h>
        > > #include <string.h>[/color]
        >[color=green]
        > > char str[9]="20051027";[/color]
        >[color=green]
        > > char year[5], month[3], day[3];[/color]
        >[color=green]
        > > int main(void){[/color]
        >[color=green]
        > > strncpy(year, str, 4);[/color]
        >
        > If you're feeling adventurous, you could use sscanf() for this task.
        > The following is easier, though...
        >
        > sprintf( year, "%.4s", str );
        > sprintf( month, "%.2s", str+4 );
        > sprintf( day, "%.2s", str+6 );
        >
        > assuming, of course, that you have somehow verified that str is in the
        > format you expect.
        >[color=green]
        > > year[4] = '\0';
        > > printf("%s\n", year);[/color]
        >
        > --
        > Christopher Benson-Manica | I *should* know what I'm talking about - if I
        > ataru(at)cybers pace.org | don't, I need to know. Flames welcome.[/color]


        Or if you want to have the same approach you can do it as below.

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

        char str[9]="20051027";

        char year[5], month[3], day[3];

        int main(void){

        printf("String str: %s\n",str);

        strncpy(year, str, 4);
        year[4] = '\0';
        printf("%s\n", year);

        strncpy(month,& str[4],2);
        month[3]='\0';
        printf("%s\n", month);

        strncpy(day,&st r[6],2);
        day[3]='\0';
        printf("%s\n", day);

        }

        Comment

        • Christopher Benson-Manica

          #5
          Re: manipulate string

          Jirka Klaue <jklaue@tkn.t u-berlin.de> wrote:
          [color=blue]
          > Well, longer. More error-prone. But easier?[/color]

          It's less tricky to do correctly than *scanf(), at least as has been
          shown by some recent posts.

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • crovax75@hotmail.com

            #6
            Re: manipulate string


            Christopher Benson-Manica wrote:[color=blue]
            > Jirka Klaue <jklaue@tkn.t u-berlin.de> wrote:
            >[color=green]
            > > Well, longer. More error-prone. But easier?[/color]
            >
            > It's less tricky to do correctly than *scanf(), at least as has been
            > shown by some recent posts.
            >
            > --
            > Christopher Benson-Manica | I *should* know what I'm talking about - if I
            > ataru(at)cybers pace.org | don't, I need to know. Flames welcome.[/color]

            Hi all,

            It works well with:

            sprintf( year, "%.4s", str );
            sprintf( month, "%.2s", str+4 );
            sprintf( day, "%.2s", str+6 );

            I want to thanks all of you for the fast replies!

            Marcel

            Comment

            • Dave Thompson

              #7
              Re: manipulate string

              On 27 Oct 2005 08:35:46 -0700, "Gow" <gowdish@gmail. com> wrote:
              <snip>[color=blue]
              > char str[9]="20051027";
              >
              > char year[5], month[3], day[3];
              >
              > int main(void){
              >
              > printf("String str: %s\n",str);
              >
              > strncpy(year, str, 4);
              > year[4] = '\0';
              > printf("%s\n", year);
              >
              > strncpy(month,& str[4],2);
              > month[3]='\0';
              > printf("%s\n", month);
              >
              > strncpy(day,&st r[6],2);
              > day[3]='\0';
              > printf("%s\n", day);
              >[/color]
              Nit: month[2] and day[2].
              [color=blue]
              > }[/color]

              - David.Thompson1 at worldnet.att.ne t

              Comment

              Working...