C语言函数strtol和strtok如何用
C语言函数strtol和strtok的用法以下:
long strtol(const char *nptr, char **endptr, int base)
示例代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345abc";
char *endptr;
long num = strtol(str, &endptr, 10);
if (endptr == str) {
printf("No digits were found.
");
}
else {
printf("The number is: %ld
", num);
printf("The next character is: %s
", endptr);
}
return 0;
}
char *strtok(char *str, const char *delim)
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello,World,How,Are,You";
char *token;
token = strtok(str, ",");
while (token != NULL) {
printf("%s
", token);
token = strtok(NULL, ",");
}
return 0;
}
TOP