Solved

How do I seperate this ?

Posted on 1998-03-12
9
181 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

828 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