Solved

Formatting a string

Posted on 2002-03-17
10
208 Views
Last Modified: 2010-04-01
I'm new in Visual C++. I want to format a string, that represents a date.

For example this string comes like "03172002". I want to set it to "17/03/2002".

I've been checking the C string functions and I haven't found any function that can help me with this.

Thanks in advance
0
Comment
Question by:juanr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Expert Comment

by:lidorc
ID: 6874921
Just run over it and format it yourself, it's very simple and the fastest solution
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6875566
char *string = "03172002";
printf("%.2s/%.2s/%.4s",string+2,string,string+4);
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 6875851
well this is to reformat it yourself.

{
char * date = "03172002";

char * dd;
char * mm;char * yy;
int c=0,i;

for(i=0;i<2;c++,i++)
   mm[i]=date[c];
for(i=0;i<2;c++,i++)
   dd[i]=date[c];for(i=0;i<4;c++,i++)
   y[i]=date[c];
}

lhn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:griessh
ID: 6876772
To format the date in VC use the "strftime" function. It allows you display the date in a multitude of ways. Please check you help for this function, that's easier than to post the chapter here.

======
Werner
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6878786
Is the data in an STL std:string variable or are you using MFC and its CString class?  Or is the string simple a char*
?

-- Dan
0
 

Author Comment

by:juanr
ID: 6886522
It's a simple char*
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6887113
Then ozo's printf idea is perhaps the best for you.  Here is rewrite in which you pass the input string to a function and it modifies it 'in place' (to statisfy your request:

>>I want to set it to "17/03/2002".

// note: make sure that pszDate is at least 11 bytes long
//
void MmDdYyyyToDdMmYyWithSlashes( char* pszDate )
{
     char szTmp[11];
     sprintf( szTmp, "%.2s/%.2s/%s",
          &szDate[2], // the DD
          &szDate[0], // the MM
          &szDate[4] // the YYYY
     );
     strcpy( pszDate, szTmp )
}

And here is an example useage:
 
    char szDate[11]= "03172002";
    MmDdYyyyToDdMmYyWithSlashes( szDate );

-- Dan

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6924220
hi juanr,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955407
Dear juanr

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "ozo"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979098
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question