-1

I'm having some problem with my code. I need to use strtok() in c to output the words "Sing" and "Toy" (which are both in between the words "Due" and "De") in the string "Date WEEk Dae Due Toy De Dae i Date Due Sing De". I tried to use the if statement found in the code to explicitly output the words "Sing" and "Toy" but my code would not produce any output and it had no warnings during compilation. I'm only a beginner at C so please be patient with me. I heard that other functions such as strstr() might be able to do the same job as strtok() so if those other functions are much more convenient to use, do not hesitate to use those functions instead. Thank you. Summary: I'm trying to get the words in between "Due" and "De" in the string above using strtok() and is it possible to do so or should I use another function?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char string[]="Date WEEk Dae    Due Toy De    Dae i Date    Due Sing De";
char*pch;
pch=strtok(string,"De");

while(pch!=NULL){
  if((*(pch-1)=='a')&&(*(pch-2)=='u'))
    printf("%s\n",pch);

    pch=strtok(NULL,"De");
}
return 0;
}
Antonio
  • 1
  • 2

4 Answers4

1

Keep in mind that the second parameter of strtok() is a delimeter list:

C string containing the delimiter characters. These can be different from one call to another.

They way it's now in your code, the token will be taken after each capital D and lower case e. For the case mentioned in your description, it's more suitable to workaround the problem using strstr().

Gnqz
  • 3,292
  • 3
  • 25
  • 35
0

you should pass " " as second argument to strtok

If you want to print Sing, check if pch is not null and strcmp(pch,"Sing") == 0 then print Sing

Pras
  • 4,047
  • 10
  • 20
  • Is this for the first, second or both instances of strtok that should have "" as a 2nd argument? – Antonio May 08 '17 at 08:09
0

Find "Due" followed by a space

char *due = strstr(string, "Due ");

Find "De" preceded by a space

char *de = strstr(string, " De");

Check for errors

if (!due || !de) exit(EXIT_FAILURE);

Print what is between

printf("%.*s\n", (int)(de - due - 4), due + 4);
pmg
  • 106,608
  • 13
  • 126
  • 198
0

Use strstr like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void){
    char string[]="Date WEEk Dae    Due Toy De  Dae i Date  Due Sing De";
    char *pre_word = "Due", *post_word = "De";
    size_t pre_word_len = strlen(pre_word), post_word_len = strlen(post_word);
    char *p = string, *pre, *post;

    while(pre = strstr(p, pre_word)){//find pre word
        if((pre == string || isspace((unsigned char)pre[-1])) &&
           isspace((unsigned char)pre[pre_word_len])){//word check
            if(post = strstr(pre + pre_word_len, post_word)){//find post word
                if(isspace((unsigned char)post[-1]) &&
                  (isspace((unsigned char)post[post_word_len]) || !post[post_word_len])){//word check
                    *post = 0;//The original string is changed
                    char word[32], dummy[2];
                    if(1==sscanf(pre + pre_word_len, "%31s %1s", word, dummy)){//There is one word between words
                        printf("'%s'\n", word);
                    }
                }
                p = post + post_word_len;//set next search position
            } else {
                break;//Since post_word  does not exist, it ends loop.
            }
        }
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70