Hey guys I have been trying to expand the following code but have had no luck, is there anyway I could also make this code say if the previous character is space or a is comma? Thanks.

//temp is the array ch is a char pointer
if( ch == temp || isspace (*(ch-1)))
char ch = temp[ i - 1]; // i is the current position ; i must be greater than 0
if( ch == ' ' || ch == '\'')

or

// i is the current position ; i must be greater than 0
if( temp[ i - 1]== ' ' || temp[ i - 1]== '\'')

hi thanks for the reply I couldn't get it to work here is the code at the moment it deals with if it is at the start or previous character is whitespace but i also need the or comma clause.

while((ch = strstr( ch, "test ")) != 0)
	{
							
		if( ch == temp || isspace (*(ch-1)))
		{
			count++;
			ch++;								 
			
		}
							
			else
			 {
							
			break;
			}
		}
#include <iostream>
int main()
{
	char* test = "testing is a'test string";
	char* ch = test;
	int count = 0;
	while( (ch = strstr( ch, "test")) != 0)
	{
		if ( ch != test )// Check if you are at the beginning of the string
		{
			ch--;
			if( ch[ 0 ] == ' ' || ch[ 0 ] == '\'')
			{
				count++;
				ch ++;
				// Output the result
				std::cout << count << std::endl;
				std::cout << ch << std::endl;
				ch ++;
			}
		}
		else
		{
			count++;
			std::cout << count << std::endl;
			std::cout << ch << std::endl;
			ch++;
		}
	}
	return 0;
}

Modify for your requirements.

>>it is at the start or previous character is whitespace but i also need the or comma clause.


just add more || operators on that line!

if( ch == temp || isspace (*(ch-1)) || *(ch-1) == ',')

or you could use a switch statement

switch(*(ch-1))
{
  case ' ':
  case '\t':
  case ',':
   // blabla
   break;
}

Thanks for you help guys, in my program at the moment I am trying to count int & char paramters, I have the code that starts the array after the open brace and I am using the code below to give the the parameter count but for some reason the count is wrong, it says I have 8 ints and 4 chars any idea where i went wrong? Thanks.

//text file contents
int test (int f, int k, char u,int f, int a, char h)
while((ch = strstr( ch, "int ")) != 0)
	{
							
		if( ch == temp || isspace (*(ch-1)) || *(ch-1) == ',')
			{
			    intCount++;
			   ch++;								 
			
			}
							
				else
					 {
							
					break;
					}
						}


		ch=temp;

	while((ch = strstr( ch, "char ")) != 0)
		{
							
		if( ch == temp || isspace (*(ch-1)) || *(ch-1) == ',')
		{
			charCount++;
			ch++;								 
			
		}
							
		else
			 {
							
			break;
			}
						
}

see if this helps.

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

int getCount(char* string,char* dataType)
{
	int counter = 0;
	char* ch = string;
	while((ch = strstr( ch, dataType)) != 0)
	{
							
		if( ch && ch == string || isspace (*(ch-1)) || *(ch-1) == ','|| *(ch-1) == '(')
		{
			counter++;
			// bypass everything up to the next space
			while(!isspace(*ch))
				ch++;								 
		}						
		else
		{						
			break;
		}
	}
	return counter;
}

int main(int argc, char* argv[])
{
	char str[] = "int foo(int f, int k, char u,int f, int a, char h)";
	int intCount;
	int charCount;
	intCount = getCount(str,"int ");
	charCount = getCount(str,"char ");
	
	printf("intCount: %d charCount: %d\n",intCount,charCount);
	return 0;
}

Can you give me any advice acient dragon?

Can you give me any advice acient dragon?

Yes, learn to read and comprehend what you read. I posted working code for you. Compare it with what you have to see what you are doing wrong.

thanks I will look into it now

I am still getting the wrong number of ints (8) and chars(4) the only difference between the code I tested and the code you have is that I started the array after the first brace and the array data was obtained from a text file so it was:

int f, int k, char u,int f, int a, char h)

I am also not sure what the following does?

ch && ch == string

Could the problem be with the way I am using arrays or does it matter that some parameters may have whitespace after the comma and some don't?

Thanks again.

>>the only difference between the code I tested and the code you have is that I started the array after the first brace and the array data was obtained from a text file so it was:


No it isn't. Check the loop very carefully -- the if statement is larger and there is a while statement you do not have.

repost your current program.

>>or does it matter that some parameters may have whitespace after the comma and some don't?

That should not be a problem because the if statement is checking for both conditions.

Hi this is my code I have the longer if and extra while loop.

I am still using the same test data and it says there is 8 ints when there should only be 4 as I started the array just after the first brace.

void test(char* temp)
{


	char * ch=temp;

		
	while((ch = strstr( ch, "int ")) != 0)
	{
							
	if( ch && ch == temp || isspace (*(ch-1)) || *(ch-1) == ','|| *(ch-1) == '(')
		{
			iCount++;
								    
                       while(!isspace(*ch))
									
			ch++;								 
			
		}
							
				else
				  {
							
					break;
				}
      }

cout<<"no of ints: "<<icount<<endl;
}

Thanks I have got it sorted but I was just wondering what the following part of the code does:

if(ch && ch == string

thanks again

char[]//set the number of required in the memory eg
char bradley[10]

Thanks I have got it sorted but I was just wondering what the following part of the code does:

if(ch && ch == string

thanks again

remove the "ch &&" -- its reduncent and not needed. That just checks to see that ch != NULL, but it can't be NULL when it reaches the if satement due to the line above it.

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.