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
... 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