473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Nov 15 '05 #1
6 1490
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Paul | last post by:
This method: static void swap(int a, int b) { int c = a; a = b; b = c; } does not swap the values of a and b over. It doesn't work for String variables either. However the following method...
14
by: Chris | last post by:
I heard that strings are immutable, but isn't there regardless a way to manipulate a string? I have a string that looks like this: a = '0123456789' But I want it to look like this: a = '0 - 2 -...
5
by: Greg Scharlemann | last post by:
If I have a date that looks like: 2005-12-07 10:10:00 How could I manipulate it in php to say "Dec, 07, 2005"? I can separate the string at the space, but don't know where to go from...
1
by: tom | last post by:
Hi, I am a newbie and have a simple question? I have a aspx file and on the form I have a user control which is my header and has some images. Now for various pages I need to load different...
4
by: G | last post by:
Hello, Does anyone have any code handy which will take a certain part of a string and save it as an independant string? For example: "Thank you for visiting our web site. You were sent...
0
by: Bart | last post by:
Hi, i want to programmatically manipulate the property 'Name' of a ControlParameter inside a InsertParameters tag. This the aspx code: ------------------ <asp:SqlDataSource...
1
by: Imnew2c | last post by:
need help with logic of a pgm... pgm is to print the following output... This computer generated string contains ___letters:,____ A,_____B, ....._____Z. This is the output. The blanks are...
3
by: redx | last post by:
void addition::print_output() { cout << "Enter value : "; getline(cin,store); } ----------------------------------------------------------------------- here my problem, for e.g : if user...
5
by: sachin770 | last post by:
i am a beginner .can you give me idea how we can manipulate "LARGE" integers (positive or negative) of arbitrary size. These integers could be greater than MAX_INT or smaller than MIN_INT. using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.