Solved

How do I seperate this ?

Posted on 1998-03-12
9
171 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
Comment Utility
You can use the strtok function and check for the length of the returned strings.
0
 

Author Comment

by:singhtaj
Comment Utility
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
Comment Utility
Well, actually strtok() serves as a tokenizer; a scan seems more appropriate here.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
Comment Utility
:)

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
Comment Utility
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
Comment Utility
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
Comment Utility
Hey, ozo, what about this? ;)
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
getting it to work may prove instructive;)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 while-loops in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now