Strip String in 2 blocks

S

Senger

Hello,

I need separete a string in 2 blocks.
For example:

Which is my favourite fruit?|Orange
Which is my favourite color?|Blue

I try to use string.h functions, but always return problems.
My last tentative was:

if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}

The question is: How I can take the second period, with the answer??

Thanks!
 
S

stathis gotsis

Senger said:
Hello,

I need separete a string in 2 blocks.
For example:

Which is my favourite fruit?|Orange
Which is my favourite color?|Blue

I try to use string.h functions, but always return problems.
My last tentative was:

if ((fp = fopen(filelong,"r"))!=NULL){
while (fgets(sentence,sizeof(sentence),fp)){
asking=strtok(sentence,"|");
//Return "Which is my
favourite fruit?"
}

The question is: How I can take the second period, with the answer??

Thanks!

If every line of the input file is like: string1|string2, and | does not
exist in string1 (or string2), you can use strchr() to determine where '|'
is in the line. A relevant code snipet would be:

char *str1,*str2;

str1=sentence;
str2=strchr(sentence,'|');
if (str2!=NULL)
{
*str2='\0';
str2++;
}
else
{
/*Do something else*/
}

Then you can use str1,str2 as you wish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top