Hans En
Hans En

Reputation: 926

Compare Chars read from stdin?

I am trying to read a line of tokens from the Keyboard. These are saved in a char[]. The tokens are seperated by a space. Using stok() I can split the string, but I need to evaluate the tokens for further actions. My Code:

char tString[50];

if (fgets(tstring, sizeof tstring, stdin)) {
            printf("%s", tstring);
            }



        char delimiter[] = " ";
        char *ptr;

        // initialisieren und ersten Abschnitt erstellen
        ptr = strtok(tstring, delimiter);

        while(ptr != NULL) {
            printf("Abschnitt gefunden: %s\n", ptr);
            // naechsten Abschnitt erstellen
            ptr = strtok(NULL, delimiter);
            if(ptr != '-'){
                puts("HIT MINUS!");
            }
        }

I can´t get the output HIT MINUS after entering 2 3 - as my Input line. Thanks for any help.

Upvotes: 0

Views: 479

Answers (4)

Pavlus
Pavlus

Reputation: 1711

char tstring[50];

if (fgets(tstring, sizeof tstring, stdin)) {
            printf("%s", tstring);
            }



        char delimiter[] = " ";
        char *ptr;

        // initialisieren und ersten Abschnitt erstellen
        ptr = strtok(tstring, delimiter);

        while(ptr != NULL) {
            printf("Abschnitt gefunden: %s\n", ptr);
            // naechsten Abschnitt erstellen
            if(*ptr == '-'){  // "=="
                puts("HIT MINUS!");
            };
            // moved to the end. Now you won't dereference NULL anyway.
            // and it will work if minus is the last token in string
            // and first returned by strtok()
            ptr = strtok(NULL, delimiter); 
        }

Upvotes: 0

Constantin
Constantin

Reputation: 8961

You have to dereference the pointer before you compare it - but also check against NULL there.

Also your char tString[50]; should be char tstring[50]; in order to compile.

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

int main (int argc, char **argv){
  char tstring[50];
  char delimiter[] = " ";
  char * ptr;
  if (fgets(tstring, sizeof tstring, stdin)) {
    printf("%s", tstring);
    // initialisieren und ersten Abschnitt erstellen
    ptr = strtok(tstring, delimiter);
    while(ptr != NULL) {
      printf("Abschnitt gefunden: %s\n", ptr);
      // naechsten Abschnitt erstellen
      ptr = strtok(NULL, delimiter);
      if(ptr!= NULL && *ptr != '-'){
        puts("HIT MINUS!");
      }
    }
  }
}

Upvotes: 0

P.P
P.P

Reputation: 121387

You are comparing the pointer here:

        if(ptr != '-'){

You want to compare the value:

        if(ptr && *ptr != '-'){

During the last iteration (last token) ptr will be NULL and you can't dereference NULL. So I altered the condition slightly.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881403

ptr is a pointer to a character. If you want to get at the character that it points to, use *ptr:

if (*ptr == '-') {
    puts ("HIT MINUS!");
}

You also appear to have the equality check the wrong way around as well. I suspect it should be == (is equal to) rather than != (is not equal to), so I've changed it to that.

You may also find yourself dereferencing the NULL pointer at some time so you may want to check for that as well:

if ((ptr != NULL) && (*ptr == '-')) {
    puts ("HIT MINUS!");
}

Upvotes: 2

Related Questions