By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

manipulate string

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
cr******@hotmail.com wrote:
#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);


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #2

P: n/a
Christopher Benson-Manica:
cr******@hotmail.com:
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
Nov 15 '05 #3

P: n/a
Gow
Christopher Benson-Manica wrote:
cr******@hotmail.com wrote:
#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);


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

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);

}

Nov 15 '05 #4

P: n/a
Jirka Klaue <jk****@tkn.tu-berlin.de> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5

P: n/a

Christopher Benson-Manica wrote:
Jirka Klaue <jk****@tkn.tu-berlin.de> wrote:
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.

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


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

Nov 15 '05 #6

P: n/a
On 27 Oct 2005 08:35:46 -0700, "Gow" <go*****@gmail.com> wrote:
<snip>
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
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.