Solved

Formatting a string

Posted on 2002-03-17
10
203 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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