473,654 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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___________ _________\nPara meters = %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 1853
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********@gmai l.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___________ _________\nPara meters = %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.ne t
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4005
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 **stringp, const char *delim);
2
2772
by: songie D | last post by:
What is the difference between these two functions?
1
6806
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 database then call the __doPostBack function of the window.opener. This has been working like a charm for the last several months. Our ISP where we run the .NET app recently upgraded to 1.1 of the framework. When I run my app at the ISP...the...
5
1522
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 <string> #include <iostream> #include <map> #include <sstream> class CCSI_Env
5
1667
by: Johs32 | last post by:
I have the follwing code: #include <stdio.h> #include <stdlib.h> struct data { int *ip; };
5
1713
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 kind of use)? template <typename E> class Mytest { public: Mytest(int n) { s = E(0);
2
1785
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: Percent Decimal Places: 0 Column Count: 0
121
5083
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
3131
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 that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.