Solved

Formatting a string

Posted on 2002-03-17
10
194 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now