Usage of strstr() and strtol() functions

1. The strstr(str1,str2) function is used to determine whether the string str2 is a substring of str1. If so, the function returns the address of the first occurrence of str2 in str1; otherwise, it returns NULL.

example

The following example demonstrates the usage of strstr() function.

example

#include <stdio.h>
#include <string.h>
 
 
int main()
{
   const char haystack[20] = "RUNOOB";
   const char needle[10] = "NOOB";
   char *ret;
 
   ret = strstr(haystack, needle);
 
   printf("子字符串是: %s\n", ret);
   
   return(0);
}

Let's compile and run the above program, which will produce the following result:

The substring is: NOOB

 2、 strtol

The strtol function converts the parameter nptr string into a long integer according to the parameter base, and the parameter base ranges from 2 to 36.

long int strtol(const char *nptr,char **endptr,int base);

Function: Used to replace strings with long integers

Parameters: char *nptr is the string to be converted

The parameter base ranges from 2 to 36, or 0. The parameter base represents the base system used. If the base value is 10, base 10 is used; if base value is 16, base 16 is used. When the base value is 0, it will use decimal for conversion, but when it encounters a prefix character such as '0x', it will use hexadecimal conversion, and when it encounters a '0' prefix character instead of '0x', it will Use octal for conversion.

At the beginning, strtol() will scan the parameter nptr string, skip the preceding space characters, and start conversion until it encounters a number or a positive and negative symbol, and then end the conversion when it encounters a non-number or the end of the string ('\0') , and returns the converted value. The parameter endptr points to the position where the conversion is stopped. If all characters of the string nptr are successfully converted into numbers, endptr points to the string terminator '\0'. To judge whether the conversion is successful, check whether **endptr is '\0'.

Guess you like

Origin blog.csdn.net/luyao3038/article/details/127301892