<String.h> 总结:
常用的函数:
一、memchr:
说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
代码:
#include <stdio.h>
void *memchr(const void*s, int c, size_t n){
const unsigned char uc = c;
const unsigned char *su;
for(su = s; 0 < n; ++su, --n)
if(*su == uc)
return ((void *)su);
return (NULL);
}
int main(int argc, char const *argv[])
{
char*s ="I am Levi !";
char *p;
p = memchr(s, '!', 11);
if(p) printf("%s ", p);
else printf("Not Found! ");
return 0;
}
// find first occurrence if c in s[n].
二、memcmp:
说明:比较内存区域buf1和buf2的前count个字节
代码:
#include <stdio.h>
int memcmp(const void *s1, const void *s2, size_t n){
const unsigned char *su1, *su2;
for(su1 = s1, su2 = s2; 0 < n; ++su1, ++su2, --n)
if(*su1 != *su2)
return ((*su1 < *su2) ? -1 : +1);
return 0;
}
int main(int argc, char const *argv[])
{
char*s1 ="Cello kitty1";
char*s2 ="Dello kitty";
int r;
r = memcmp(s1, s2, 11);
if(!r) printf("s1 and s2 are identical ");
else if(r < 0) printf("s1 less than s2 ");
else printf("s1 greater than s2 ");
return 0;
}
//compare unsigned char (s1[n] and s2[n]).
三、memcpy
说明:由src所指内存区域复制count个字节到dest所指内存区域
代码:
#include <stdio.h>
void *memcpy(void *s1, const void *s2, size_t n){
char *su1;
const char *su2;
for(su1 = s1, su2 = s2; 0 < n; ++su1, ++su2, --n)
*su1 = *su2;
return (s1);
}
int main(int argc, char const *argv[])
{
char*s ="Hello Levi";
char d[11];
memcpy(d, s, 10);
printf("%s ", d);
puts(d);
return 0;
}
/* copy char s2[n] to s1[n] in any order */
四、memmove
说明:
1、由src所指内存区域复制count个字节到dest所指内存区域。
2、src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。
代码:
#include <stdio.h>
void *memmove(void *s1, const void *s2, size_t n){
char *sc1;
const char *sc2;
sc1 = s1;
sc2 = s2;
if(sc2 > sc1 && sc1 < sc2 + n)
for(sc1 += n, sc2 += n; 0 < n; --n)
*--sc1 = *--sc2;
else
for(; 0 < n; --n)
*sc1++ = *sc2++;
return (s1);
}
int main(int argc, char const *argv[])
{
char s[] ="wushirenfei shitaiyanliang";
memmove(s, s+12, 15);
printf("%s ", s);
return 0;
}
/* copy char s2[n] to s1[n] safely */
五、memset
说明:
1、把buffer所指内存区域的前count个字节设置成字符c。
2、返回指向buffer的指针。
代码:
#include <stdio.h>
void *memset(void *s, int c, size_t n){
const unsigned char uc = c;
unsigned char *su;
for(su = s; 0 < n; ++su, --n)
*su = uc;
return (s);
}
int main(int argc, char const *argv[])
{
char s[100] ="Bu xiang yan hua lu shang xing";
memset(s+1, 'a', 1);
printf("%s ", s);
return 0;
}
六、strncat、strcat
说明:
1、strcat 把src所指字符串添加到dest结尾处并添加' '。
2、src、dest 不可以重叠
代码:
#include <stdio.h>
char *strncat(char *s1, const char *s2, size_t n){
char *s;
for(s = s1; *s != ' '; ++s)
;
for(; 0 < n && *s2 != ' '; --n)
*s++ = *s2++;
*s = ' ';
return (s1);
}
char *strcat(char *s1, const char *s2){
char *s;
for(s = s1; *s != ' '; ++s)
;
for(; (*s = *s2) != ' '; ++s, ++s2)
;
return s1;
}
int main()
{
char d[100]="Golden Global";
char *s=" View WinIDE Library";
strcat(d,s);
printf("%s ",d);
return 0;
}
七、strcpy、strncpy
说明:
1、把src所指由NULL结束的字符串复制到dest所指的数组中
2、src和dest所指内存区域不可以重叠
代码:
#include <stdio.h>
char *strcpy(char *s1, const char *s2){
char *s;
for(s = s1; (*s++ = *s2++) != ' '; )
;
return (s1);
}
char *strncpy(char *s1, const char *s2, size_t n){
char *s;
for(s = s1; 0 < n && *s2 != ' '; --n)
*s++ = *s2++;
for(; 0 < n; --n)
*s++ = ' ';
return s1;
}
int main(int argc, char const *argv[])
{
char*s ="Levi is Hacker";
char d[100];
strcpy(d, s);
printf("%s ", d);
return 0;
}
八、strcmp、strncmp
说明:
1、比较字符串s1和s2
2、如何比较,看代码吧
代码:
#include <stdio.h>
int strncmp(const char *s1, const char *s2, size_t n){
for(; 0 < n; ++s1, ++s2, --n)
if(*s1 != *s2)
return((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
else if(*s1 == ' ')
return 0;
return 0;
}
int strcmp(const char *s1, const char *s2){
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == ' ')
return 0;
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
}
int main(int argc, char const *argv[])
{
char *s1="Ze";
char *s2="Hel";
int r;
r = strcmp(s1, s2);
if(!r)
printf("s1 and s2 are identical ");
else
if(r<0)
printf("s1 less than s2 ");
else
printf("s1 greater than s2 ");
return 0;
}
九、strlen
说明:返回字符串长度
代码:
#include <stdio.h>
size_t strlen(const char*s){
const char *sc;
for(sc = s; *sc != ' '; ++sc)
;
return sc - s;
}
int main(int argc, char const *argv[])
{
int n;
char *s ="Hello Levi";
n = strlen(s);
printf("%d ", n);
return 0;
}
十、strchr
说明:
1、查找s中首次出现字符c位置
2、返回指针,指向c出现位置,否则返回NULL
代码:
#include <stdio.h>
char *strchr(const char *s, int c){
const char ch = c;
for(; *s != ch; ++s)
if(*s == ' ')
return NULL;
return (char *)s;
}
int main(int argc, char const *argv[])
{
char *s = "Golden Global View";
char *p;
p = strchr(s,'i');
if(p)
printf("%s ", p);
else
printf("Not Found! ");
return 0;
}
十一、strcspn
说明:
1、在字符串s1中搜索s2出现的字符。
2、返回找到字符的下标值
代码:
#include <stdio.h>
size_t strcspn(const char *s1, const char *s2){
const char *sc1, *sc2;
for(sc1 = s1; *sc1 != ' '; ++sc1)
for(sc2 = s2; *sc2 != ' '; ++sc2)
if(*sc1 == *sc2)
return (sc1 - s1);
return (sc1 - s1);
}
int main(int argc, char const *argv[])
{
char *s="Golden Global View";
char *r="L";
int n = strcspn(s,r);
printf("%d ", n);
return 0;
}
十二、strrchr
说明:
1、在字符串搜索c字符
2、返回指针,指向搜索c字符最后出现的字符下标
代码:
#include <stdio.h>
char *strrchr(const char *s, int c){
const char ch = c;
const char *sc;
for(sc = NULL; ; ++s){
if(*s == ch)
sc = s;
if(*s == ' ')
return ((char *)sc);
}
}
int main(int argc, char const *argv[])
{
char *s = "Hello LLLLevi";
char *p = strrchr(s, 'L');
printf("%s ", p);
return 0;
}
十三、strstr
说明:
1、返回父串完全匹配子串的指针
2、返回的指针指向完全匹配子串的position
代码:
#include <stdio.h>
char *strchr(const char *s, int c){
const char ch = c;
for(; *s != ch; ++s)
if(*s == ' ')
return NULL;
return (char *)s;
}
char *strstr(const char *s1, const char *s2){
if(*s2 == ' ')
return ((char *)s1);
for(; (s1 = strchr(s1, *s2)) != NULL; ++s1){
const char *sc1, *sc2;
for(sc1 = s1, sc2 = s2; ; )
if(*++sc2 == ' ')
return ((char *)s1);
else if(*++sc1 != *sc2)
break;
}
return NULL;
}
int main(int argc, char const *argv[])
{
char *s = "ccdd View";
char *l = "dd";
char *p;
p=strstr(s,l);
if(p)
printf("%s ", p);
else
printf("Not Found!");
return 0;
}
Ps:深圳冬天还有蚊子,擦擦,蚊子太多了,不写了,回宿舍睡觉觉~~