Solved

Formatting a string

Posted on 2002-03-17
10
207 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path to  STL Map header file 1 91
Android development question 2 108
parameter pack in c++11 2 21
COM server issue 2 21
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

737 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