1.strlen 函数
函数原型:
size_t strlen ( const char * str );
需要引入头文件:string.h
获取字符串长度
下面一段英文摘自http://www.cplusplus.com/reference/cstring/strlen/
This should not be confused with the size of the array that holds the string. For example:char mystr[100]="test string";
defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
In C++, char_traits::length implements the same behavior.
大致说,这个函数返回字符串实际长度,而在C++里char_traits::length 实现了同样的功能!!!
看以下代码:
#include
#include
int mylen( char *);
int main(int argc,char const * argv[] )
{
char firstName[] = "zhang";
printf("strlen = %lu\n",strlen(firstName));
printf("mylen = %lu\n",mylen(firstName));
printf("sizeof = %lu\n",sizeof(firstName));
return 0;
}
int mylen( char *s)
{
int idx = 0;
while( s[idx] != '\0'){
idx++;
}
return idx;
}
运行结果:
[root@ C]# ./strlen
strlen = 5
mylen = 5
sizeof = 6
因为在C语言中,字符串是以'\0'结尾,所以,在mylen函数可以改造成如下:
int mylen( char *s)
{
int idx = 0;
while( s[idx]){
idx++;
}
2. strcmp
函数原型:
int strcmp ( const char * str1, const char * str2 );
以下摘自于:http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
意思是说,这个函数从每个字符串第一个字符开始比较,当它们彼此相同则继续往下比较,直到不同或者遇到结束符。
函数返回值:
Returns an integral value indicating the relationship between the strings:
return value | indicates |
---|---|
<0 |
the first character that does not match has a lower value in ptr1 than in ptr2 |
0 |
the contents of both strings are equal |
>0 |
the first character that does not match has a greater value in ptr1 than in ptr2 |
代码:
#include
#include
int mycmp(const char *s1,const char*s2);
int mycmp_pointer(const char *s1,const char * s2);
int main(int argc,char const *argv[])
{
char s1[] = "abc";
char s2[] = "bbc";
printf("%d\n",strcmp(s1,s2));
printf("%d\n",mycmp(s1,s2));
printf("%d\n",mycmp_pointer(s1,s2));
return 0;
}
int mycmp(const char *s1,const char*s2)
{
int idx = 0;
while(s1[idx] == s2[idx] && s1[idx] != '\0'){
idx++;
}
return s1[idx] - s2[idx];
}
int mycmp_pointer(const char *s1,const char *s2)
{
while(*s1 == *s2 && *s1 != '\0'){
s1++;
s2++;
}
return *s1 - *s2;
}
~
运行结果:
[root@ C]# ./strcmp
-1
-1
-1
在上面代码中,mycmp 是数组版本,mycmp_pointer是指针实现版本,while循环的条件是字符串1 和字符串2 的字符相同并且字符串s1没结束。
3.strcpy
函数原型:
char * strcpy ( char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
复制字符串到另一个字符串,包含结束符。
需要包含头文件:string.h
代码:
#include
#include
char* mycpy (char * dest,char const *src) ;
char* mycpy_pointer(char * dest,char const * src);
int main(int argc,char const * argv)
{
char *charStr1 = "this is a test ,ok";
char charStr2[] = "hello,good afternoon";
char charStr3[] = "test sub string";
printf("in strcpy : charStr2 = %s\n",strcpy(charStr2,charStr1));
printf("in mycpy : charStr3 = %s\n",mycpy(charStr3,"copy by mycpy"));
printf("in mycpy_pointer : charStr3 = %s\n",mycpy_pointer(charStr3,"copy by mycpy_pointer"));
return 0;
}
char* mycpy (char * dest,char const *src)
{
int idx = 0;
while( src[idx])
{
dest[idx] = src[idx];
idx++;
}
dest[idx] = '\0';
return dest;
}
char* mycpy_pointer(char * dest,char const * src)
{
char * ret = dest;
while(*src ){
*dest = *src;
*src++;
*dest++;
}
*dest = '\0';
return ret;
}
运行结果:
[root@ C]# ./strcpy
in strcpy : charStr2 = this is a test ,ok
in mycpy : charStr3 = copy by mycpy
in mycpy_pointer : charStr3 = copy by mycpy_pointer
上面的代码,当循环结束后,一定要加结束符:'\0'
在my_pointer函数里还可以写的更简洁些:
char* mycpy_pointer(char * dest,char const * src)
{
char * ret = dest;
while(*dest++ = *src++ ) ;
*dest = '\0';
return ret;
}
4.strchr
函数原型:
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
Returns a pointer to the first occurrence of character in the C string str.
返回指向字符串str中第一次出现chracter的指针
看代码:
#include
#include
#include
int main(int argc,char const * argv)
{
char str[] = "hello";
//获取str中l的部分字串
char *p = strchr(str,'l');
printf("查找第一个l:%s\n",p);
//获取l前面部分字符串
char charTemp = *p;
*p = '\0';
char *tempChar = (char*)malloc(strlen(str) + 1 );
strcpy(tempChar,str);
printf("获取第一个l前面的部分字符串 str = %s\n",tempChar);
*p = charTemp;
//获取第二个l及后面的子串
p = strchr(p + 1 ,'l');
printf("获取第二个l及后面的子串 %s\n",p);
//复制字符串
char *t = (char*)malloc(strlen(p) + 1);
strcpy(t,p);
printf("复制字符串 %s\n",t);
//释放内存
free(t);
return 0;
}
运行结果:
[root@ C]# ./strchr
查找第一个l:llo
获取第一个l前面的部分字符串 str = he
获取第二个l及后面的子串 lo
复制字符串 lo
在上面的代码中,第8行,即char str[] = "hello";这行中,之前写的是char * str = "hello";然后后面编译运行报段错误。
编译运行(为了调试方便在编译的时候加上调试参数):
[root@ C]# gcc -o strchr strchr.c -g
[root@ C]# ./strchr
查找第一个l:llo
Segmentation fault
[root@ C]# gdb strchr
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
Reading symbols from /home/riche/C/strchr...done.
(gdb) run
Starting program: /home/riche/C/strchr
查找第一个l:llo
Program received signal SIGSEGV, Segmentation fault.
0x00000000004006ed in main (argc=1, argv=0x7fffffffe5f8 "'\350\377\377\377\177") at strchr.c:17
17 *p = '\0';
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7.x86_64
(gdb)
一直百思不得其解,为什么写成数组形式就ok,而写成指针形式就报段错误。