By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,676 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,676 IT Pros & Developers. It's quick & easy.

Wierd behaviour of function 'strcspn'

P: n/a
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[i] = 0;

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

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

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
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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[i])

anyway .. thanks for your attention ..

Nov 14 '05 #2

P: n/a
On 17 Dec 2004 09:01:32 -0800, pr********@gmail.com (Vaddina Prakash
Rao) wrote:

<snip>
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[i] = 0;

for (i=0, s1=string; i<no_of_param; i++)
{
s[i] = 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++, so I wouldn't worry. </>
s1 = ++s[i];
--s[i];
Why not just s1 = s[i] + 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[i]);
}

for (i=0; i<no_of_param; i++)
{
printf ("\ns[%d] = %s\n", i, s[i]);
++s[i];
count[i] = strcspn(s[i], "%");
if (count[i])
count[i] = strcspn(s[i], "\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[i] to a completely wrong value. Your followup
change to if(!count[i]) works, because count[i] 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[i]);
--s[i];
}
}

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[i] = s[i+1] - s[i];
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
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.