combination of a string and queries

T

Tagore

I have written following recursive function for finding combinations
of a string str with i characters.

void combi(char *str,int i)
{
int len=strlen(str);
static char cstring[20]; //this string stores combinations
static int cur_len; //current lenght of combination
int k;
if(i==1) //base case
{
for(k=0;k<len;k++)
{
cstring[cur_len++]=str[k];
cstring[cur_len]='\0'; //query-1 here

printf("%s\n",cstring);

cur_len--;
}
return;
}

for(k=0;k<=len-i ;k++)
{
int temp=cur_len;
cstring[cur_len++]=str[k];
combi(str+k+1,i-1);
cur_len=temp;
}
}


QYERIES:
1) Static variables are initialized with 0 by default. Are static char
array also initialized with all 0 which is equivalent to string nul
terminator? so if i do not explicitly assign cstring end with '\0' it
would be fine?

2) I am using 2 static members in my function. problem is that there
life time remains for while of my program thus using resources and
also my function would not work if i call it another time(i can avoid
it by initalizing cur_len with 0).
Is there any better method to do above program and get rid of static
members.
 
T

Tagore

I have written following recursive function for finding  combinations
of a string str with i characters.
please note that i is number of alphabets we have to select from our
string(combinations)
 
U

user923005

I have written following recursive function for finding  combinations
of a string str with i characters.

void combi(char *str,int i)
{
        int len=strlen(str);
        static char cstring[20]; //this string stores combinations

If the length of cstring ever exceeds 20 (including the trailing nul
byte) then you will have undefined behavior.
        static int cur_len;      //current lenght of combination
        int k;
        if(i==1)    //base case
        {
                for(k=0;k<len;k++)
                {
                        cstring[cur_len++]=str[k];
                        cstring[cur_len]='\0';  //query-1 here

                        printf("%s\n",cstring);

                        cur_len--;
                }
                return;
        }

        for(k=0;k<=len-i ;k++)
        {
                int temp=cur_len;
                cstring[cur_len++]=str[k];
                combi(str+k+1,i-1);
                cur_len=temp;
        }

}

QYERIES:
1) Static variables are initialized with 0 by default. Are static char
array also initialized with all 0 which is equivalent to string nul
terminator?

Yes. Here is what the standard says about initialization:
ISO/IEC 9899:1999 (E) ©ISO/IEC
Semantics
8 An initializer specifies the initial value stored in an object.
9 Except where explicitly stated otherwise, for the purposes of this
subclause unnamed members of objects of structure and union type do
not participate in initialization. Unnamed members of structure
objects have indeterminate value even after initialization.
10 If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate. If an object that has static
storage duration is not initialized explicitly, then:
* if it has pointer type, it is initialized to a null pointer;
* if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
* if it is an aggregate, every member is initialized (recursively)
according to these rules;
* if it is a union, the first named member is initialized
(recursively) according to these rules.
11 The initializer for a scalar shall be a single expression,
optionally enclosed in braces. The initial value of the object is that
of the expression (after conversion); the same type constraints and
conversions as for simple assignment apply, taking the type of the
scalar to be the unqualified version of its declared type.
12 The rest of this subclause deals with initializers for objects that
have aggregate or union type.
13 The initializer for a structure or union object that has automatic
storage duration shall be either an initializer list as described
below, or a single expression that has compatible structure or union
type. In the latter case, the initial value of the object, including
unnamed members, is that of the expression.
14 An array of character type may be initialized by a character string
literal, optionally enclosed in braces. Successive characters of the
character string literal (including the terminating null character if
there is room or if the array is of unknown size) initialize the
elements of the array.
15 An array with element type compatible with wchar_t may be
initialized by a wide string literal, optionally enclosed in braces.
Successive wide characters of the wide string literal (including the
terminating null wide character if there is room or if the array is of
unknown size) initialize the elements of the array.
16 Otherwise, the initializer for an object that has aggregate or
union type shall be a brace enclosed list of initializers for the
elements or named members.
17 Each brace-enclosed initializer list has an associated current
object. When no designations are present, subobjects of the current
object are initialized in order according to the type of the current
object: array elements in increasing subscript order, structure
members in declaration order, and the first named member of a union.
127) In contrast, a designation causes the following initializer to
begin initialization of the subobject described by the designator.
Initialization then continues forward in order, beginning with the
next subobject after that described by the designator.128)
18 Each designator list begins its description with the current object
associated with the closest surrounding brace pair. Each item in the
designator list (in order) specifies a particular member of its
current object and changes the current object for the next designator
(if any) to be that member.129) The current object that results at the
end of the designator list is the subobject to be initialized by the
following initializer.
19 The initialization shall occur in initializer list order, each
initializer provided for a particular subobject overriding any
previously listed initializer for the same subobject; all subobjects
that are not initialized explicitly shall be initialized implicitly
the same as objects that have static storage duration.
20 If the aggregate or union contains elements or members that are
aggregates or unions, these rules apply recursively to the
subaggregates or contained unions. If the initializer of a
subaggregate or contained union begins with a left brace, the
initializers enclosed by that brace and its matching right brace
initialize the elements or members of the subaggregate or the
contained union. Otherwise, only enough initializers from the list are
taken to account for the elements or members of the subaggregate or
the first member of the contained union; any remaining initializers
are left to initialize the next element or member of the aggregate of
which the current subaggregate or contained union is a part.
21 If there are fewer initializers in a brace-enclosed list than there
are elements or members of an aggregate, or fewer characters in a
string literal used to initialize an array of known size than there
are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.
22 If an array of unknown size is initialized, its size is determined
by the largest indexed element with an explicit initializer. At the
end of its initializer list, the array no longer has incomplete type.
23 The order in which any side effects occur among the initialization
list expressions is unspecified.130)
footnote: 127) If the initializer list for a subaggregate or contained
union does not begin with a left brace, its subobjects are initialized
as usual, but the subaggregate or contained union does not become the
current object: current objects are associated only with brace-
enclosed initializer lists.
footnote: 128) After a union member is initialized, the next object is
not the next member of the union; instead, it is the next subobject of
an object containing the union.
footnote: 129) Thus, a designator can only specify a strict subobject
of the aggregate or union that is associated with the surrounding
brace pair. Note, too, that each separate designator list is
independent.
footnote: 130) In particular, the evaluation order need not be the
same as the order of subobject initialization.
so if i do not explicitly assign cstring end with '\0' it
would be fine?

2) I am using 2 static members in my function. problem is that there
life time remains for while of my program thus using resources and
also my function would not work if i call it another time(i can avoid
it by initalizing cur_len with 0).

Using static values means that it is not reentrant.
Is there any better method to do above program and get rid of static
members.

You can pass in arrays from above.
Unless I do not understand it (a strong possibility), I don't think
your algorithm works.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top