本文共 4015 字,大约阅读时间需要 13 分钟。
size_t strlen(const char* str);
#includeint main(){ const char* str = "abcdef"; printf("%d\n", strlen(str)); return 0;}
执行结果
6
char* strcpy(char* destination, const char* source);
#include#include int main (){ char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0;}
执行结果
str1: Sample string
str2: Sample string str3: copy successful
// 函数:char* strcat(char* destination, const char* source_);#include#include int main (){ char str[80]; strcpy (str,"these "); strcat (str,"strings "); strcat (str,"are "); strcat (str,"concatenated."); puts (str); return 0;}
these strings are concatenated.
int strcmp(const char* str1, const char* str2)
#include#include int main (){ char key[] = "apple"; char buffer[80]; do { printf ("Guess my favorite fruit? "); fflush (stdout); scanf ("%79s",buffer); } while (strcmp (key,buffer) != 0); puts ("Correct answer!"); return 0;}
执行结果
Guess my favourite fruit? orange
Guess my favourite fruit? apple Correct answer!
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
#include#include int main (){ char str[] ="This is a simple string"; char * pch; pch = strstr (str,"simple"); if (pch != NULL) strncpy (pch,"sample",6); puts (str); return 0;}
执行结果
This is a sample string
char * strtok ( char * str, const char * delimiters );
#include#include int main (){ char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0;}
char * strerror(int errnum);
#include#include #include int main (){ FILE * pFile; pFile = fopen ("unexist.ent","r"); if (pFile == NULL) printf ("Error opening file unexist.ent: %s\n",strerror(errno)); return 0;}
执行结果
打开文件不存在时出错:没有这样的文件或目录
函数 | 符合下列条件就为真 |
---|---|
isdigit | 十进制0-9 |
islower | 小写字母a-z |
isupper | 大写字母A-Z |
isalpha | 字母a-z或A-Z |
函数 | 作用 |
---|---|
tolower | 转换成小写字母 |
toupper | 转换成大写字母 |
void * memcpy ( void * destination, const void * source, size_t num );
#include#include struct { char name[40]; int age;} person, person_copy;int main (){ char myname[] = "Pierre de Fermat"; /* using memcpy to copy string: */ memcpy ( person.name, myname, strlen(myname)+1 ); person.age = 46; /* using memcpy to copy structure: */ memcpy ( &person_copy, &person, sizeof(person) ); printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age ); return 0;}
执行结果
person_copy: Pierre de Fermat, 46
void * memmove ( void * destination, const void * source, size_t num );
#include#include int main (){ char str[] = "memmove can be very useful......"; memmove (str+20,str+15,11); puts (str); return 0;}
执行结果
memmove can be very very useful.
void * memcpy ( void * destination, const void * source, size_t num );
返回值 | 表示 |
---|---|
< 0 | 两个内存块中不匹配,第一个字节在ptr1中的值比在ptr2中的值低 |
= 0 | 两个内存块的内容相等 |
> 0 | 两个内存块中不匹配,第一个字节在ptr1中的值大于在ptr2中的值 |
转载地址:http://ssacz.baihongyu.com/