manipulate string

C

crovax75

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
 
C

Christopher Benson-Manica

#include <stdio.h>
#include <string.h>
char str[9]="20051027";
char year[5], month[3], day[3];
int main(void){
strncpy(year, str, 4);

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.
year[4] = '\0';
printf("%s\n", year);
 
J

Jirka Klaue

Christopher Benson-Manica:
(e-mail address removed):
char str[9]="20051027";
....
If you're feeling adventurous, you could use sscanf() for this task.

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);
The following is easier, though...

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

Well, longer. More error-prone. But easier?
assuming, of course, that you have somehow verified that str is in the
format you expect.

OK, sscanf can do this for you.

Jirka
 
G

Gow

Christopher said:
#include <stdio.h>
#include <string.h>
char str[9]="20051027";
char year[5], month[3], day[3];
int main(void){
strncpy(year, str, 4);

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.
year[4] = '\0';
printf("%s\n", year);


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,&str[6],2);
day[3]='\0';
printf("%s\n", day);

}
 
C

Christopher Benson-Manica

Jirka Klaue said:
Well, longer. More error-prone. But easier?

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

crovax75

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

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
 
D

Dave Thompson

On 27 Oct 2005 08:35:46 -0700 said:
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,&str[6],2);
day[3]='\0';
printf("%s\n", day);
Nit: month[2] and day[2].

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top