473,322 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Wierd behaviour of function 'strcspn'

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
2 1837
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: daniel | last post by:
Hi everyone, I'm trying to get this program compiled under Solaris. Unfortunately I have little experience with C. Solaris doesn't use the function strsep() anymore: char *strsep(char...
2
by: songie D | last post by:
What is the difference between these two functions?
1
by: paul reed | last post by:
Hello, I am having some weird behavior between two machines...one which is running the 1.1 framework and one which is running 1.0. After opening a child form from a parent...I update the...
5
by: subaruwrx88011 | last post by:
I am very new to c++ and very new to maps. Below is the source of my program. The output is very strange. Am I inserting correctly into the map? Any help will be greatly appreciated. #include...
5
by: Johs32 | last post by:
I have the follwing code: #include <stdio.h> #include <stdlib.h> struct data { int *ip; };
5
by: desktop | last post by:
I am confused about the use of the template parameter "E" in the below class. Since when is it allowed to use these parameters like "E(1)" and what does it mean (where can I read more about this...
2
by: RamanS | last post by:
I have created a combo box with the following properties: RowSourceType: Value List RowSource: "0%";"10%";"25%";"36%";"37%" Bound to column: 1 Limit to List = Yes Default Value = None Format:...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.