Solved

Formatting a string

Posted on 2002-03-17
10
189 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 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.

706 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

17 Experts available now in Live!

Get 1:1 Help Now