Saturday, July 7, 2007

Write a C program to implement the strlen() function.

The prototype of the strlen() function is...

size_t strlen(const char *string);

Here is some C code which implements the strlen() function....

int my_strlen(char *string)
int length;
for (length = 0; *string != '\0', string++)

Also, see another example

int my_strlen(char *s)
char *p=s;




Anonymous said...

What about when char* is NULL?

M Shoaib Sheikh said...

Try this out !

int mystrlen(char const *s)
if(*s==0) return 0;
return mystrlen(s+1)+1;

Manish Awasthi said...

what if the string contains unicode characters??

Clearer said...

IMHO the best implementation of strlen given here is the second one in the original post (with a check for null somewhere before the while loop). You may want to use const char* instead of char* though, and possibly const char * restrict -- it allows the compiler to do better optimizations, which is good.

C characters are not defined to be unicode characters -- even if you are using unicode characters (UTF-8) 0 still terminates a string.

If you are sing other unicode characters (UTF-16 or uTF-32) then simply, define strlen thus:

int strlen_utf16(const short * s);
int strlen_utf32(const int * s);

Otherwise use any strlen implementation.

I would -strongly- advice against using the recursive implementation unless you know that all strings are short. Large strings leads to a very large number of function calls, which is not just bad for perfomance but will eventually lead to crashing.

Ofcourse, you could go about redefining strings altogether, but if you do so, you shouldn't be asking here.

Anonymous said...

The Lyford Cay Sect serves as a community resource as far as something intercontinental and Bahamian families. Lyford Cay College offers a enormous par Nursery entirely Piercing Inculcate education. Through a multi-cultural circumstances that promotes reconciliation amongst multiform nationalities, students are provided with the opportunities and resources to develop cross-cultural learners and front-office citizens. Lyford Cay Followers is an undecided, non-denominational, hour school. The coterie is a non-profit-making object governed around a Directorship of Directors.

Uttam Agrawal said...

Sample C Program To Accept A String & Display It.

Sample C Program To Find The Length Of A String.

Sample C Program To Concatenate Two Strings.

Sample C Program To Compare Two Strings.

Sample C Program To Swap Two Strings.

Sample C Program To Swap Two Strings Using strcpy() Function.

Sample C Program To Sort A Given Number Of Strings Using strcmp() Function.

Sample C Program To Check Whether A String Is Palindrome Or Not.

Sample C Program To Print The Reverse Of A String.

Sample C Program To Join Two Strings.

Sample C Program To Display Array Of Strings.

Sample C Program To Convert String To An Integer Using atoi() Function.

Sample C Program To Accept A String & Display In Reverse.

Sample C Program To Accept A String & Display Its Alternate Characters.

Sample C Program To Accept A String & Display Alternate Characters In Either Case.

Anonymous said...

Hi, all is going fine here and ofcourse every one is sharing data,
that's genuinely good, keep up writing.

My weblog: height to waist ratio