I am doing a lexical analysis program and this is a part of the code that counts number of keywords

int keyword=0;
    char str[]="int float + - 78";
    char* ptr;

    ptr=strtok(str," ");

    while(ptr!=NULL)
    {
   if(((strcmp(ptr,"int"))||(strcmp(ptr,"float")))==0)
   keyword++;                               
   ptr=strtok(NULL," ");
    }     
    cout<<keyword;

to my knowledge there is no imbalance of parantheses in the if statement and the tokening loop also works fine ...the desired output is 2 but the output here is 0....can anyone help
thanks in advance

Recommended Answers

All 8 Replies

badly formatted condional statement

if( (strcmp(ptr,"int")== 0) || (strcmp(ptr,"float") == 0) )

Properly formatted:

if (((strcmp(ptr,"int")) || (strcmp(ptr,"float")))  ==  0)

No it is not. That does not work. Example:

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

int main(int argc, char* argv[])
{
	char *ptr = "float";

	if (((strcmp(ptr,"int")) || (strcmp(ptr,"float")))  ==  0)
		printf("Yes\n");
	else
		printf("no\n");
	return 0;
}

I apologize Walt -- I did not intend to edit your post. My screwup! :(

Member Avatar for iamthwee

I apologize Walt -- I did not intend to edit your post. My screwup! :(

The mind plays tricks on you when you get to your age.

The mind plays tricks on you when you get to your age.

Uncalled for and in no way relevant to the topic. Please refrain from personal comments Mr. Iamthwee.

Uncalled for and in no way relevant to the topic. Please refrain from personal comments Mr. Iamthwee.

I'm sure it was all in good fun, and hopefully AD will take it that way. Wee-wee doesn't have a habit of taking nasty jabs at other members. :)

I'm sure it was all in good fun, and hopefully AD will take it that way. Wee-wee doesn't have a habit of taking nasty jabs at other members. :)

Hmm... had no way of knowing it, just was doing my job so that the situation doesnt get out of hand.

Well anyways continue with your good natured humour :)

Member Avatar for iamthwee

Uncalled for and in no way relevant to the topic. Please refrain from personal comments Mr. Iamthwee.

I didn't think it was personal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.