Solved

How do I seperate this ?

Posted on 1998-03-12
9
176 Views
Last Modified: 2010-04-02
If I have a program where :
char *X= "Fri, 27 Feb 1998" ;

How would I access "Fri", "27", "Feb" "1998" individually ?
Given the fact I don't  know the number of blank spaces
between them ???

Any ideas ..
Any help will be appreciated.

Thanks
0
Comment
Question by:singhtaj
9 Comments
 
LVL 3

Expert Comment

by:elfie
ID: 1257674
You can use the strtok function and check for the length of the returned strings.
0
 

Author Comment

by:singhtaj
ID: 1257675
char *X="Fri, 27 Feb 1998";
char *wd = strtok(X," ,");
char *md = strtok(0," ,");
char *mon = strtok(0," ,");
char *year = strtok(0," ,");
printf("%s, %d %s %d\n",wd,atoi(md),mon,atoi(year));
0
 
LVL 84

Expert Comment

by:ozo
ID: 1257676
Well, actually strtok() serves as a tokenizer; a scan seems more appropriate here.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 5

Expert Comment

by:julio011597
ID: 1257677
char *X="Fri, 27 Feb 1998";
char *wd = (char *)malloc(strlen(X));
char *mon = (char *)malloc(strlen(X));
int md,year;
sscanf(X,"%[^, ]%*c%d %s %d",wd,&md,mon,&year);
0
 
LVL 84

Expert Comment

by:ozo
ID: 1257678
:)

But... shouldn't be:

char *wd = malloc(strlen(X) + 1);

(also, the typecast is discouraged if you are writing - and compiling - ANSI-C code, because fools the compiler; it is needed by C++)
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1257679
Sorry. In my haste I neglected to #include <stdlib.h> or <malloc.h> to declare malloc,
and casting was the easiest way to get rid of the warnings.

I thought of strlen(X)+1 too, but figured as long as at least one other field matched...

Maybe strdup(X) would have been simpler.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1257680
you may use strtok() or you may write a small fnc as shown below.(please include stdio.h and malloc.h)
char *[] FormatDate(char *SrcDate){ static char *FormattedStrings[4];int i=j=k=len=0;
for(;SrcDate[i] && j<4;j++,k=i,len=0)
{ for(;SrcDate[i] && SrcDate[i]!=' ' && SrcDate[i]!=',';i++,len++);
FormattedStrings[j]=(char*)malloc(len+1);
strncpy(FormattedStrings[j],&SrcDate[k],len);}}
return FormattedStrings;}
/* the required format is stored in strings in a DD  cahr array */
0
 

Accepted Solution

by:
rsjetty earned 100 total points
ID: 1257681
Hey, ozo, what about this? ;)
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1257682
getting it to work may prove instructive;)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

770 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