Wierd behaviour of function 'strcspn'

  • Thread starter Vaddina Prakash Rao
  • Start date
V

Vaddina Prakash Rao

Hi all .. I describe here a wierd behaviour .. i dont understand why
... This could be very stupid aswell .. so please bear me ..

I have been writing a program to accept multiple parameters and print
them out .. similar to printf (please Dont ask me why this, when we
already have printf .. Because i have plans add more power to this
function). Any way i print the code here,
_____________________________________________________________________

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

void print_out(int, char *, ...);
main()
{
print_out(2, "\nChal mere Bhai %d !!! This is prakash %d", 210,
420);
}

void print_out(int no_of_param, char *string, ...)
{
int i, j, jint, count[no_of_param];
char jchar, *jstring, *s1, *s[no_of_param], *final[no_of_param];
va_list ap;
va_start(ap, string);
printf ("\n____________________\nParameters = %d", no_of_param);

for (i=0; i<no_of_param; i++)
count = 0;

for (i=0, s1=string; i<no_of_param; i++)
{
s = strstr(s1, "%");
s1 = ++s;
--s;
printf ("\ns[%d] = %s\n", i, s);
}

for (i=0; i<no_of_param; i++)
{
printf ("\ns[%d] = %s\n", i, s);
++s;
count = strcspn(s, "%");
if (count)
count = strcspn(s, "\0");
printf ("\nCount[%d] = %d\n", i, count);
--s;
}
}


__________________________________________________________

it would be better if i describe the algorithm with an example:

String: "\nChal mere Bhai %d !!! This is prakash %d"

I am in the first step of my implementation. I wish to divide the
string into as many parts as there are format specifiers. Here we have
2 format specifiers, so i wish to divide the string into 2 individual
strings.

I acheive this in the second 'for' loop. This is perfect.:
s[0] = "%d !!! This is prakash %d"
s[1] = "%d"

Since i only want one format specifier in one string array, i want to
erase the '%d' in the string-1 (s[0]). I want to do this by counting
the number of characters between the two '%d'(please check the code
for exact behaviour), So that i can use this count to copy the
characters one by one from one string to an another final string ..
(Not specified in the code above)..

And my problem is, in the second for loop, using the function,
strcspn, results in the total number of characters to be 2 more than
the actual number. The behaviour remains the same for any string that
is passed. But when i try to confirm this behaviour with an individual
function (code below), i get the right number of characters...
--------------------------------------------------------
char *s="d !!! This is Germany %d";
int s8;
s8 = strcspn (s,"%");
printf ("%d\n", s8);
----------------------------------------------------------

I dont know whats happenning .. I guess it became too long .. but i
couldnt reduce it .. i found everything important .. if you need
anything more please let me know

thanks for your time ...
prakash
 
P

prakash437

hi guys ..

well.. i solved it .. as i said it was a little typo that made this
difference ..

The if statement in the third 'for' loop need to be modified as ..
if (!count)

anyway .. thanks for your attention ..
 
D

Dave Thompson

On 17 Dec 2004 09:01:32 -0800, (e-mail address removed) (Vaddina Prakash
Rao) wrote:

void print_out(int no_of_param, char *string, ...)
{
int i, j, jint, count[no_of_param];
char jchar, *jstring, *s1, *s[no_of_param], *final[no_of_param];
va_list ap;
va_start(ap, string);
printf ("\n____________________\nParameters = %d", no_of_param);

for (i=0; i<no_of_param; i++)
count = 0;

for (i=0, s1=string; i<no_of_param; i++)
{
s = strstr(s1, "%");


You might want to check for NULL here in case the caller makes a
mistake and doesn't pass a no_of_param that agrees with the string.

<hyperpedantic>The name string is actually within the space of
str[a-z]* identifiers reserved (for future expansion) for all use if
you have #include'd <string.h> as you have; and also with external
linkage always which doesn't matter for a function parameter. In
reality this identifier is unlikely to be used, especially as it would
gratuitously conflict with C++ said:
s1 = ++s;
--s;


Why not just s1 = s + 1? Or + 2 if as appears from this example
your conversion specifiers always take at least one char after the %?
printf ("\ns[%d] = %s\n", i, s);
}

for (i=0; i<no_of_param; i++)
{
printf ("\ns[%d] = %s\n", i, s);
++s;
count = strcspn(s, "%");
if (count)
count = strcspn(s, "\0");


"\0" is actually (an array containing two null characters which is
treated as) an empty string, and strcspn (str, empty) is strlen (str)
so this sets count to a completely wrong value. Your followup
change to if(!count) works, because count will always be nonzero
unless there are consecutive % signs -- in which case this will give a
result you probably don't want -- but even better is to just delete
this whole case -- strcspn will already count up to the end of the
string if there was no further %, which is what you apparently want.
printf ("\nCount[%d] = %d\n", i, count);
--s;
}
}

Alternatively, since you have already found and remembered the
positions of all the % characters, you could just use
for( i = 0; i < no_of_params-1; i++ ) count = s[i+1] - s;
count[no_of_params-1] = strlen(s[no_of_params-1]);
And my problem is, in the second for loop, using the function,
strcspn, results in the total number of characters to be 2 more than
the actual number. The behaviour remains the same for any string that
is passed. But when i try to confirm this behaviour with an individual
function (code below), i get the right number of characters...

Your problem was "2 more" only because the rest of the string happened
to be 2 characters (%d). In general it is the rest of the string.



- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top