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 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.
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
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);
}
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 -...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: redx |
last post by:
void addition::print_output()
{
cout << "Enter value : ";
getline(cin,store);
}
-----------------------------------------------------------------------
here my problem,
for e.g : if user...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |