Question:
string length?
a
2007-11-09 02:33:58 UTC
write a program to find the length of the string without using control structures and without using string.h header files???


in c language
Four answers:
Fred W
2007-11-09 08:20:17 UTC
It's an interesting problem without "control structures". I am going to interpret this requirement as eliminating for() while() and do until() loops, as well as the "if" statement.



We will eliminate the obvious (sizeof s - 1) solution, as this does not return what strlen(s) would.



Any operator or primitive function call can be used...



So, for this solution, we will use only recursion (possibly sequencing), and expressions --



/* The strlen of a string beginning with \0 is 0,

otherwise the strlen is 1 more than the strlen of the rest

of the string . The ? operation selects the first expression if non-zero (not a \0 character), or the second expression if it is zero (a \0 character when *s is evaluated). We then evaluate 1 + the length of the rest of the string as a recusive call. It is formulated in such a way that tail-recursive optimizers (eg. gcc) should be able to convert this into a simple loop. This has been tested with CXC (C interpreter) */



int my_strlen(char *s)

{

return *s ? 1 + my_strlen(++s) : 0;

}



As a benefit, this formulation of strlen() does not require any extra variables. And, it is very concise.



We have to examine each character of the string in order to determine its length (in the sense of strlen() ). This will involve some sort of "control structure", even if that structure is simple sequencing:



/* warning, not tested */

char *s = "ab";

int len = 0, flag = 1;

len += *s != 0; flag &= *s++ != 0;

len += (*s != 0) * flag; flag &= *s++ != 0;

len += (*s != 0) * flag; flag &= *s++ != 0;

len += (*s != 0) * flag; flag &= *s++ != 0;

/* repeat the above line for each character possible */



Here, we examine each character. s[n] != 0 evaluates to 0 if its the terminal 0, and 1 if its any other character. We add to the accumulated length, and keep track of whether the terminal 0 has been seen in flag. Once flag is 0, 'and'ing with 0 or 1 will result in 0.



This solution requires one program line for each possible character in the string, and is therefore not very practical. But, it does eliminate the recursion (in favour of sequence). There can be no formulation of a solution (equivalent to strlen() in function) that involves absolutely no "control structures", if sequencing and recursion are counted as control structures. But, as the examples above prove, either recursion or sequencing is sufficient to the task.



An alternative approach is to split the problem into finding the \0 byte in the string, and then using pointer arithmetic to compute (I have not tested the code below, so buyer beware. It should be clear, though):



char *end_string(char *s)

{

return *s ? end_string(++s) : s;

}



int my_strlen(char *s)

{

return end_string(s) - s;

}



If the C compiler used is not "smart" enough to handle the first solution, it may be able to handle this one. The recursive step is simply on ++s, with no partial expression in process. Therefore, this could be rewritten as:



char *end_string(char *s)

{

top:

return *s ? ({++s; goto top;}) : s;

}



(I believe that is the GCC syntax -- I could be wrong, but it's really not expressible in ANSI C). The idea is that the compiler should detect easily that the CALL is immediately followed by a RETURN, and that this sequence can be replaced by a GOTO instead (the "essence" of tail-recursion elimination -- although this case is simple enough that it could be done with a peephole optimizer).



We can now formulate a string copy and concatenation without control structures as well:



char *my_strcpy(char *dst, char *src)

{

*dst = *src;

return *src ? my_strcpy(++dst, ++src) : dst;

}



char *my_strcat(char *dst, char *src)

{

return my_strcpy(end_string(dst), src);

}



My preference for the strlen() function is the first formulation, assuming the compiler is advanced enough.



Is this what you wanted?
zanzabar4ky7
2007-11-09 02:52:42 UTC
peniks said how to in VB and close to c# not C





what u need to do is use a counting loop that keeps going so long as it has valid input





this sounds like homework so i hope that no1 dose teh entire thing for u, but this should get u started
Peniks
2007-11-09 02:41:14 UTC
you can use sizeof(name_of_variable) - 1



sizeof function is not defined in string.h it is in iostream.h
puneeth cva
2007-11-09 02:55:58 UTC
the function is

int len(char *s)

{

int len=0,i;

while((*(s+i))!='\0')

{ len++;i++;}

len++;

return len;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...