Method of calculating string length and dividing string in C language _c language

Source: Internet
Author: User
Tags strtok

C language strlen () function: Returns the length of a string
header file:

 #include <string.h>

The strlen () function is used to calculate the length of a string, and its prototype is:

  unsigned int strlen (char *s);

"Parameter description" s is the specified string.

Strlen () is used to calculate the length of the specified string s, excluding the end character "".

Return value returns the number of characters in the string s.

Note the character array, for example

  Char str[100] = "http://see.xidian.edu.cn/cpp/u/biaozhunku/";

A character array with a size of 100 is defined, but only the first 11 characters are initialized and the rest is 0, so sizeof (str) equals 100,strlen (str) equals 11.

If the number of characters equals the size of the character array, then the return value of strlen () cannot be determined, for example

  Char str[6] = "ABCXYZ";

The return value of strlen (str) will be indeterminate. Because the end of STR is not 0,strlen () will continue to search backwards until it encounters the ' yes ', and the contents of these areas are indeterminate.

Note: the strlen () function calculates the actual length of the string, and encounters the first ' end '. If you just define not giving it an initial value, the result is indeterminate, and it will look up from the first address until it is stopped. The sizeof returns the amount of memory that is claimed after the variable declaration, not the actual length, in addition sizeof is not a function, just an operator, and strlen () is a function.

The function example gets the length of the string.

#include <stdio.h>
#include <string.h>
int main ()
{
  char *str1 = http:// See.xidian.edu.cn/cpp/u/shipin/";
  Char str2[100] = "http://see.xidian.edu.cn/cpp/u/shipin_liming/";
  Char str3[5] = "12345";
  printf ("strlen (str1) =%d, sizeof (STR1) =%d\n", strlen (str1), sizeof (STR1));
  printf ("strlen (str2) =%d, sizeof (STR2) =%d\n", strlen (str2), sizeof (STR2));
  printf ("strlen (STR3) =%d, sizeof (STR3) =%d\n", strlen (STR3), sizeof (STR3));
  return 0;
}

Run Result:

Strlen (str1) =38, sizeof (STR1) =4 strlen (
str1) =45, sizeof (STR1) =100 strlen
(str1) =53, sizeof (STR1) =5

The result of the above operation, strlen (STR1) =53 obviously wrong, 53 is meaningless.

C language Strtok () function: String segmentation
header file:

#include <string.h>

To define a function:

char * strtok (char *s, const char *delim);

Function Description: Strtok () is used to split the string into fragments. The argument s points to the string to be split, and the parameter Delim is a split string, and the Strtok () is found in the string of parameter s to the split word characters of the parameter Delim. On the first call, the Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. Each successful call returns the next segmented string pointer.

Return value: Returns the next segmented string pointer, or null if it is not already partitioned.

Example

#include <string.h>
Main () {
  char s[] = "Ab-cd:ef;gh:i-jkl;mnop;qrs-tu:vwx-y;z";
  Char *delim = "-:";
  char *p;
  printf ("%s", Strtok (S, Delim));
  while (P = strtok (NULL, Delim))
    printf ("%s", p);
    printf ("\ n");
}

Execution results:

AB CD ef;gh i jkl;mnop;qrs tu vwx y;z   //-with: Character has been replaced by \ 0 characters


 

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.