?
Solved

C String manipulation doubt

Posted on 2005-03-13
13
Medium Priority
?
205 Views
Last Modified: 2010-08-05
Hello experts,

         I have got a string in the form TESTSTRING12022004. i.e; <somestring><date in format ddmmyyyy>


I have to take the "date in format ddmmyyyy" alone in a sepearte string in the "yyyymmdd" form.(i.e; i have to extract the date part in the form  20040212 in to a seperate string from TESTSTRING12022004)
Always the string will be in the  format <somestring><date in format ddmmyyyy>



Please help me in doing this. Any c standard functions which can help me in doing this?
A code help is greatly appreciated..


Early help is greatly appreciated


Regards

Jango
0
Comment
Question by:jango_ms
[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
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13532386
Hi jango_ms,

Is the TESTSTRING always the same ... If not, is it always the same width ... If it is same or of same width, you can use

char * c = "TESTSTRING12022004";

char * date = c + strlen(c);

Cheers!
sunnycoder
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13532389
Well, assuming your string will not have errors:

/* will need this header file */
#include "memory.h"

ExtractDate(char *input, char *output)   // output must be a char array with at least 9 characters
{
      int len;
      char *pos;

      len = strlen(input);
      if (len<8) {   /* too short input */
         *output = 0;    /* empty string */
         return;
      }
      pos = input + len - 8;
      memcpy(input, pos+4, 4);
      memcpy(input+4, pos+2, 2);
      memcpy(input+6, pos, 2);
      input[8] = 0;   /* close string */
}
0
 

Author Comment

by:jango_ms
ID: 13532425
Hello,
       The input string is will not have the same width always, is can vary in width, but the last set of chars will be in format yyyymmdd(last 8 chars). What I want is extract the first set of strinsg (i.e; <teststring> ) in one seperate string and the date in yyyymmdd format in another string . The input string is TESTSTRING12022004. i.e; <somestring><date in format ddmmyyyy>

Mr.jaime_olivares I will test your set of code and will get in touch when I get the desired result.


Jango
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 13532438
Hi jango_ms,

char * c = "TESTSTRING12022004";
int len = strlen(c);

char * date = c + len - 8;

You can now copy the string between c and date (c inclusive, date exclusive) as TESTSTRING and date will point to the required date

Cheers!
sunnycoder
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13532728
#include <string>
using namespace std;

int main()
{
      
      char * c = "TESTSTRING12022004";

      int dd, mm, yyyy;

      c = c + (strlen(c) - 8);
      sscanf(c , "%2d%2d%d", &dd, &mm, &yyyy);
      printf("%d %d %d\n", dd, mm, yyyy);

      return 0;
}

_novi_
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 13532820
You can try this algorithm:
1. mystr = "TESTSTRING12022004"
2. int i = 0, j = 0;
3. DEFINE str1[], str2[], str3[], str4[];
4. for (i = 0; mystr[i] != '\0'; i++)
5. Do
6.    if ((mystr[i] < '0') && (mystr[i] > '9')) str1[i] = mystr[i];
7. Done
8. strncpy(str2, mystr + i, 2);
9. strncpy(str3, mystr + i + 2, 2);
10. strncpy(str4, mystr + i + 4, 4);
11. End

Here, it is assumed that:
1. Your system works on ASCII code set (so that, digits 0 to 9 are continuous).
2. The initial string doesn't contain an integer.
3. The date part is of the format ddmmyyyy.

Finally the variable str1, contains the initial string.
The variable str2, contains the date (dd) part.
The variable str3, contains the month (mm) part.
The variable str4, contains the year (yyyy) part.

Now if you want, you can concatenate this back in any sequence you want.
And you will have to convert this pseudo-code to C program (which I think you are capable of doing)

Hope this helps.

-Narendra
0
 

Author Comment

by:jango_ms
ID: 13533141
Hello Mr.SunnyCoder,
                                 As per ur early comment


You can now copy the string between c and date (c inclusive, date exclusive) as TESTSTRING and date will point to the required date



I can't yet get the TESTSTRING part in a string. Please help me in that. I was able to take the date i correct format.


Regards

Jango_ms
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 13533150
Hi jango_ms,

use strncpy ... strncpy (buffer, c, len-8);
buffer[len-8]='\0';

Cheers!
sunnycoder
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13533810
Original question is:
>I have to take the "date in format ddmmyyyy" alone in a sepearte string in the "yyyymmdd" form
Notice that d/m/y order is inverted.
So, original sunnycoder question:
>char * c = "TESTSTRING12022004";
>char * date = c + strlen(c);
is not valid.
Neither second answer:
>char * c = "TESTSTRING12022004";
>int len = strlen(c);
>char * date = c + len - 8;
>You can now copy the string between c and date
Neither last answer:
> use strncpy ... strncpy (buffer, c, len-8);
>buffer[len-8]='\0';

So, I claim the author has not choosen the 'wrong answer', as mentioned in the MA.

0
 

Author Comment

by:jango_ms
ID: 13533811
Helo sunnycoder,
                         Can u help me in a another thing.
How can we calculate the difference between 2 dates .
i.e; I have to calculate the number of days between one date (yyyymmdd format) to another date(yyyymmdd format).
Any idead, any code help??/


Regards

Jango_ms
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13533907
Sorry I meant:
 I claim the author has choosen the 'wrong answer', as explained in the MA

About date differences, have a look to this previous question:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20394126.html
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13534139
Jamie,

>(i.e; i have to extract the date part in the form  20040212 in to a seperate string from TESTSTRING12022004)
I interpreted that to mean user wants to split it into two components "TESTSTRING" and the date ... You interpreted it to mean user wants to split date into each individual components ... In this case, as it turns out, my interpretetion was correct, hence the disposition

jango_ms,

Since these two questions are unrelated, ideally you should open a separate thread for asking it. In any case, Jamie has been nice to give you a link to the solution.

Cheers!
sunnycoder
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13578038
I am talking about this:
>(i.e; i have to extract the date part in the form  20040212 in to a seperate string from TESTSTRING12022004)
Both dates are in different order, so, it is not a simple split but a reordered date number...
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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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