C language to calculate the length of a string and the method of string segmentation

  • 2020-04-02 03:17:11
  • OfStack

Strlen () returns the length of a string
The header file:


 #include <string.h>

The strlen() function is used to calculate the length of a string.


  unsigned int strlen (char *s);

S is the specified string.

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

Returns the number of characters in the string s.

Notice the array of characters, for example


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

An array of characters of size 100 is defined, but only the first 11 characters are initialized and the rest are 0, so sizeof(STR) is equal to 100 and strlen(STR) is equal to 11.

If the number of characters is equal to the size of the array of characters, the return value of strlen() cannot be determined, for example


  char str[6] = "abcxyz";

The return value of strlen(STR) will be uncertain. Because STR does not end in 0, strlen() continues to retrieve backwards until it encounters '\0', and the contents of these areas are uncertain.

Note: the strlen() function calculates the actual length of the string and ends with the first '\0'. If you just define it and don't give it an initial value, the result is indeterminate, and it will keep looking for the first address until it hits '\0'. Sizeof returns the amount of memory that the variable has been declared, not the actual length, and sizeof is not a function, just an operator, strlen() is a function.

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)=%dn", strlen(str1), sizeof(str1));
  printf("strlen(str2)=%d, sizeof(str2)=%dn", strlen(str2), sizeof(str2));
  printf("strlen(str3)=%d, sizeof(str3)=%dn", strlen(str3), sizeof(str3));
  return 0;
}

Operation results:


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

The result of the above operation is that strlen of str1 is equal to 53.

C language strtok() function: string segmentation
The header file:


#include <string.h>

Definition function:


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

Strtok () is used to split a string into segments. Parameter s points to the string to be segmented, while parameter delim is a segmentation string. When strtok() finds the segmentation character of parameter delim in the string of parameter s, it will change this character to \0 character. On the first call, strtok() must give the parameter s string, and subsequent calls set the parameter s to NULL. Each successful call returns the next split string pointer.

Return value: returns the next split string pointer, or NULL if it is not possible to split.

sample


#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   //- and: characters have been replaced by 0 characters


 


Related articles: