Hey guys I am using code like the following:

But I would like to say if a semi colon appears at any stage after a ")" (closing bracket) add one to count.

Any advice thanks. ( I am also using a char array and not a string, unfortunitly I have too)

char *ptr = array;

if(ptr = strstr(ptr, ";"))
       {
        count++;
       }

Recommended Answers

All 11 Replies

>But I would like to say if a semi colon appears at any stage
>after a ")" (closing bracket) add one to count.
Where does a closing paren (brackets are '') come into this? Are you not providing us with enough information to properly help you?

I am currently making a program that counts function prototypes in source code stored in a text file very basic program and source code in the text file.

At the moment I am counting the prototypes for example

int k (int j, char c);

but if there was a for loop written like the following:

for (int i=0; i<sizeof(array); i++)

my program would count that for loop as a prototype as my program first looks for the int then an open brace and finally a semi colon. The code works by reading in a line at a time and this is stored in a char array (has to be this way) But I can't see anyway round this problem any advice would be great.
( sorry I was talking about "(" before not "[" )
Thanks.

I believe I will need some kind of pointer that will be able to pick out that the last element is a semicolon but I am not sure how this would work as the semicolon could be the last character but there could still be space left in the array.

For example

char array[1000]="int K (int j);" ;

It sounds like you aren't being detailed enough in your parser. If you don't mind not allowing for 100% of the variations, you can assume that a function declaration will consist of an unknown number of whitespace characters followed by a valid type, and an identifier. Then look for an argument list (everything inside can be ignored) and a trailing semicolon, all on the same line. Let's say something like this:

char *strip_ws ( char *p )
{
  while ( *p != '\0' && isspace ( *p ) )
    ++p;

  return p;
}

int is_proto ( char *p )
{
  int rc = 1;

  p = strip_ws ( p );

  if ( !is_type ( p ) ) rc = 0;
  if ( !is_ident ( p ) ) rc = 0;
  if ( !is_arglist ( p ) ) rc = 0;

  p = strip_ws ( p );

  if ( *p != ';' )
    rc = 0;

  return rc;
}

That breaks your problem down into simpler steps where you identify types, identifiers, and argument lists.

Thanks Narue, When you say not allowing for 100% of the variations do you mean like a new line etc?

Thanks.

>When you say not allowing for 100% of the variations do you mean like a new line etc?
I mean short of writing a full declaration parser to account for the hundreds of ways you can format a prototype.

Hi Narue thanks again for your reply, my program does not have to be that detailed. (Which is lucky!) I was thinking about other ways this could be done I was thinking about reading in a line at a time checking for an int and an open brace and then clearing the whitespace from the array and then seeing if the last character in the array is a semicolon. Do you think that is a good idea or could that cause any more problems? (like counting something else as mentioned in my first post)

>checking for an int and an open brace and then clearing the
>whitespace from the array and then seeing if the last character
>in the array is a semicolon
Let's see:

int x = (a + b) * c;

Your suggestion sounds incomplete.

Thanks again narue, if I also did a search for the "=" would that solve the problem or is there anymore that would then cause?

>if I also did a search for the "=" would that solve the problem
Not if you're going to do what I'm thinking you'll do. Here's a case where that fails:

int x = (a + b) * c, foo(void);

You'll have similar problems if you do any searching. A search will likely skip over important information, so you'll need to step through the string and look for specifics...much like the code I gave you. That gives you a much better chance of succeeding.

Thanks again narue, I am a bit confused by your code could you explain it for me please.

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.