Solved

How do I seperate this ?

Posted on 1998-03-12
9
189 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
[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
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
Industry Leaders: 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 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

Industry Leaders: 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

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…
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.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

634 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