here is one way to do it. It just has the string hard-coded, but you should be able to easily make it work with data file. Also assumes int is at the beginning of the string -- you should be able to change it to recognize int in the middle of the string too.

your program will not work if there is a variable name that contains the string "int" but is not an int data type. Example: "float myint". your program will see myint as the int. you need to check for a space following int -- "int ", and also check for a space preceeding or beginning-of-line. example: "float myint " is not what you want even though there is a space following text "int".

Thanks, I have to use arrays for this code but with the strstr function if i use "Int " it will bring up the wrong number of ints.

If you have time could you please update where I have went wrong with the piece of code so it will be able to work and then that will give me a great idea of what else I will be able to do using arrays in this way.

Im kind of confused now does the code you posted, take care of both the "float myint" pproblems i see how it takes care of the first one by checking for a space after the int but im not sure how it deals with the problem of "float myint "

Didn't your mother ever tell you that can't never did anything? try a little harder, and don't expect to do it all in one line of code. After getting pointer from strstr, check the character just before it to see if it is either the beginning of the line or a space. If it is neither, then it isn't the "int" keyword.

Thanks for your help that was very cleaver coding, im not sure of the syntax: isspace(*(ptr-1))) but I am going to look into that in more detail.

I am still having the problem that if two ints are declared on the same line i.e. int j; int k; the second one will be ignored I have tried to use a pointer to keep track of the place in the array for example

one way to do it is to use strtok() to split the original string up with the semicolon. Then use strstr() in the pointer returned by strtok(). That way you only have to work with one statement at a time.

*(ptr-1) does nothing more than back up the pointer one character and reference the char at that location.

Hi I have just two questions about the code below when using char *ptr = strstr(line,"james smith "); does this mean that the if statement:
if( ptr == line || isspace(*(ptr-1))) is no longer needed?

I was also wondering is the comment is correct in saying if ptr is at the beginning of the line because doesn't ptr just return a pointer to the first place it finds james smith, so how could it know if it is at the beginning of the line or not?

Hi I have just two questions about the code below when using char *ptr = strstr(line,"james smith "); does this mean that the if statement:
if( ptr == line || isspace(*(ptr-1))) is no longer needed?

what about the string "mrjames smith" ? will you accept a pointer to that too? If not, then that code is still needed.

I was also wondering is the comment is correct in saying if ptr is at the beginning of the line because doesn't ptr just return a pointer to the first place it finds james smith, so how could it know if it is at the beginning of the line or not?

not certain what you are asking, but if ptr == line, then ptr is pointing to the first character in line. This is testing the address of ptr and line, not comparing the two strings.

what about the string "mrjames smith" ? will you accept a pointer to that too? If not, then that code is still needed.

Hi for that example I would not use a pointer so does that mean that the code: if( ptr == line || isspace(*(ptr-1)))
would then ignore that string? and wouldn't it do this anyway without the line of code. (sorry im a bit confused)

not certain what you are asking, but if ptr == line, then ptr is pointing to the first character in line. This is testing the address of ptr and line, not comparing the two strings.

Ah i see I was getting confused I thought it pointed to the first instance of james smith and not the start of the line so if there was something in the string before james smith for example "his name was james smith" it wouldn't be the start of the line.

Ah i see I was getting confused I thought it pointed to the first instance of james smith and not the start of the line so if there was something in the string before james smith for example "his name was james smith" it wouldn't be the start of the line.

That's the purpose of if( ptr == line) -- to see if ptr starts at the beginning of the line or somewhere in the middle, as in your example. But in the string "his name was mrjames smith", the check *(ptr-1) == ' ' will not be true because the character just befor the 'j' in 'james' is 'r', not a space.

Thanks for that, I have used similar code to search for FOR loops how ever if someone writes a for loop like the following with no whitespace after the for:
for(int k=0; k<10; k++)

I thought I would need another if to searh for "for(int" but for some reason the code does what i need anyway and for some reason i dont even need the second lot of if statments below which check for "for(" it will still find it even though there is no whitespace after the for. Can you please tell me why this is the case? Thanks

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...