Help with code
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)))
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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]== '\'')
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
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;
}
}
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
#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.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
>>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;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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;
}
}
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Can you give me any advice acient dragon?
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
thanks I will look into it now
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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.
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
>>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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;
}
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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
Jon182
Junior Poster in Training
91 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343