一、memchr函数,字符定位。 Locate character in block of memory
//1、memchr函数,字符定位。 Locate character in block of memory // void * memchr ( const void *, int, size_t ); char * pch; char str[] = "Example string"; pch =(char *) memchr(str,'p',strlen(str)); //返回的指针 if(pch != NULL){ //找不到,返回NULL cout<<pch<<endl; // 《《输出: ple string cout<<"'p'在位置:"<<pch-str+1; //《《输出: 'p'在位置:5 //指针在加减运算后,输出为数字 (下标) }
二、memcmp函数 比较两个字符串占内存的大小。
int memcmp ( const void * ptr1, const void * ptr2, size_t num ); num为比较的字节数
char str1[256]; char str2[256]; int n; gets(str1); //gao gets(str2);//tong n = memcmp(str1, str2, 256); //256为比较的字节数,一个字符占一byte cout<<n<<endl; //输出:-1 return 0;
三、memcpy函数, 复制指定的字节数,返回复制的目的 地址
void * memcpy ( void * destination, const void * source, size_t num );
char str1[]="Sample string"; char str2[40]; char str3[40]; memcpy (str2,str1,strlen(str1)+1); //strlen(str1)+1,因为字符串后面有'\0' memcpy (str3,"copy successful",16); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
// 输出: // str1: Sample string // str2: Sample string // str3: copy successful
四、memmove函数 Move block of memory
//memmove函数 Move block of memory char str[] = "gao love tong"; char * p; p = (char *)memmove(str+9, str+4,4); // cout<<str<<endl<<p;
//输出: // gao love love // love
五、memset函数 Fill block of memory
void * memset ( void * ptr, int value, size_t num );
char str[] = "almost every programmer should know memset!"; memset(str,'-',6); //'-'直接转换为数字存储,6是字节数 cout<<str; //输出:------ every programmer should know memset!
六、strcat 函数 ,连接两个字符串
char * strcat ( char * destination, const char * source );
七、strchr,查找字符。Locate first occurrence of character in string
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
char str[] = "this is a sample string"; char * pch; pch = strchr(str, 's'); while(pch != NULL){ cout<<"found at:"<<pch-str+1<<enl; //输出找到的字符下标 pch = strchr(pch+1,'s'); // }
found at:4 found at:7 found at:11 found at:18
八、strcmp函数,字符串比较
int strcmp ( const char * str1, const char * str2 );
char * c1; char * c2; c1 = "abd"; c2 = "abcde"; cout<<strcmp(c1,c2);
输出:1
九、strcoll函数 功能和strcmp类似
十、strcpy函数 字符串复制 十一、strstr 函数,查找子串
十二、strpbrk 匹配字符串2中的所有字符
char * strpbrk ( char * str1, const char * str2 );
char str[] = "This is a sample string"; char key[] = "aeiou"; char * pch; printf ("Vowels in '%s': ",str); pch = strpbrk (str, key); while (pch != NULL) { printf ("%c " , *pch); pch = strpbrk (pch+1,key); } printf ("\n");
output:
Vowels in 'This is a sample string': i i a a e i
十三、strrchr 查找最后出现的字符。 和 strchr相反 bfs dfs acm
|
请发表评论