Evaluate the string length strlen (array pointer)

Source: Internet
Author: User

Problem:

Evaluate the number (including spaces) of characters contained in a string, that is, evaluate the string length;

# Include <stdio. h> # include <assert. h> int _ strlen (const char * Str) {assert (STR! = NULL); int I = 0; For (; * STR ++! = '\ 0'; I ++); // For (; STR ++! = NULL; I ++); // some statements can also be used, but the execution result is an endless loop. Str ++ may not be null even if it crosses the border; return I ;} int _ strlen2 (const char STR []) {assert (STR! = NULL); int I = 0; while (STR [I]! = '\ 0') I ++; return I;} int main () {char TMP [] = "dsgrfr s00w e324 SD! "; // No matter TMP [] or TMP *, the string will end with '\ 0'; int lenth = _ strlen (TMP); int len2= _ strlen2 (TMP ); printf ("the string length1 is: % d \ n", lenth); printf ("the string leng2is: % d \ n", len22.); Return 0 ;}

 

The output is as follows:

 

[[Email protected] desktop] #./A. outthe string length1 is: 22The string leng2is: 22 [email protected] desktop] #

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.