c 字符串、字符和字节 strlen、strcpy、strcat、strcmp、strchr、strstr


1、strlen  字符串长度

#include <stdio.h>
#include <string.h>

main()
{
   char *message = "hello liaojianguo";

   printf("leng=%d\n" , strlen(message));
}
输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
leng=17


2、与sizeof的区别

(1) sizeof是一个C语言中的一个单目运算符,而strlen是一个函数,用来计算字符串的长度。

(2)sizeof求的是数据类型所占空间的大小[以字节为单位],而strlen是求字符串的长度

3、特别说明有关下面 strlen(e),去找/0结尾的字符串的长度,由于找不到/0,所以返回的值是一个不确定的值

#include <stdio.h>
#include <string.h>

main()
{
char d[]="abcdef";
char e[]={'a','b','c','d','e','f'};
printf("%d\n",strlen(d));
printf("%d\n",strlen(e));
}

输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
6
10

4、strcpy  复制字符串

#include <stdio.h>
#include <string.h>

main()
{

   char message [] = "Hello,LiaoJianGuo";
   strcpy(message,"ZhuLongFeng");
   printf("string=%s\n",message);

}


#include <stdio.h>
#include <string.h>

main()
{

   char message [100] ;
   strcpy(message,"ZhuLongFeng");
   printf("string=%s\n",message);

}


输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
[sudo] password for jianguo: 
jianguo@jianguo:/work/study$ ./main
string=ZhuLongFeng


5、strcat  字符串连接

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
    char str[100] ="0123456789";   
    char *str1 ="abcdefghi";
  
    printf("string.length=%d\n",strlen(str));   

    strcat(str,str1);   

    printf("string.length=%d\n",strlen(str));   
    printf("string=%s\n", str);   
  
 }  

输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
string.length=10
string.length=19
string=0123456789abcdefghi

注意下面

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
    char str[10] ="0123456789";   
    char *str1 ="abcdefghi";
  
    printf("string.length=%d\n",strlen(str));   

    strcat(str,str1);   

    printf("string.length=%d\n",strlen(str));   
    printf("string=%s\n", str);   
  
 }  

输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
string.length=10
string.length=19
string=0123456789abcdefghi
*** stack smashing detected ***: ./main terminated
======= Backtrace: =========
/lib/libc.so.6(__fortify_fail+0x50)[0x2df980]
/lib/libc.so.6(+0xe592a)[0x2df92a]
./main[0x804851f]
/lib/libc.so.6(__libc_start_main+0xe7)[0x210ce7]
./main[0x80483e1]
======= Memory map: ========
001fa000-00351000 r-xp 00000000 08:06 2228269    /lib/libc-2.12.1.so
00351000-00352000 ---p 00157000 08:06 2228269    /lib/libc-2.12.1.so
00352000-00354000 r--p 00157000 08:06 2228269    /lib/libc-2.12.1.so
00354000-00355000 rw-p 00159000 08:06 2228269    /lib/libc-2.12.1.so
00355000-00358000 rw-p 00000000 00:00 0 
003ec000-00408000 r-xp 00000000 08:06 2228245    /lib/ld-2.12.1.so
00408000-00409000 r--p 0001b000 08:06 2228245    /lib/ld-2.12.1.so
00409000-0040a000 rw-p 0001c000 08:06 2228245    /lib/ld-2.12.1.so
004fd000-00517000 r-xp 00000000 08:06 2228303    /lib/libgcc_s.so.1
00517000-00518000 r--p 00019000 08:06 2228303    /lib/libgcc_s.so.1
00518000-00519000 rw-p 0001a000 08:06 2228303    /lib/libgcc_s.so.1
00b47000-00b48000 r-xp 00000000 00:00 0          [vdso]
08048000-08049000 r-xp 00000000 08:08 11534338   /work/study/main
08049000-0804a000 r--p 00000000 08:08 11534338   /work/study/main
0804a000-0804b000 rw-p 00001000 08:08 11534338   /work/study/main
08373000-08394000 rw-p 00000000 00:00 0          [heap]
b7855000-b7856000 rw-p 00000000 00:00 0 
b7862000-b7865000 rw-p 00000000 00:00 0 
bf9cb000-bf9ec000 rw-p 00000000 00:00 0          [stack]
已放弃
jianguo@jianguo:/work/study$ 

总结:strcpy和strcat一样,必须保证目标字符数组剩余空间足以保证整个源字符串,必须考虑目标数值中原先存在的字符串

即:原型声明:extern char *strcpy(char *dest,char *src); 
  头文件:string.h 
  功能:把src所指由NULL结束的字符串复制到dest所指的数组中。 
  说明:srcdest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。 
  返回指向dest的指针。 

6、strcmp  比较两个字符串

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
        char *s1="Hello, liaojianguo";
        char *s2="Hello, liaojianguo";
        int r;
        
        r=strcmp(s1,s2);
        if(!r)
          printf("s1 and s2 are identical\n");
        else
        if(r<0)
          printf("s1 less than s2\n");
        else
          printf("s1 greater than s2\n");
 }  


说明:如果s1小于s2,返回一个小于0的值,如果s1大于s2,函数返回一个大于0的值,如果两个字符串相等,函数就返回0.

7、类似上面的函数

char  *strncpy(char *dst,char const *src,size_t len);
char  *strcat(char *dst,char const *src,size_t len);
int  *strcmp(char *dst,char const *src,size_t len);

8、查找字符

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
    char *s="Hello, liaojianguo";
    char *p,c='j';
    p=strchr(s,c);// char *strchr(char *__s1, int __n);找第一次出现的位置,若没有返回NULL 
    if(p)
        printf("%s\n",p);
    else
        printf("Not Found!\n");
 }  
输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
jianguo

9、查找字符串

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
    char *s="Hello, liaojianguo";
    char *c="ll";
    char *p;
    p=strstr(s,c);
    if(p)
        printf("%s\n",p);
    else
        printf("Not Found!\n");
 }  


输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
llo, liaojianguo

10、剩下的其他的就不尝试了,其主要看标准库,字符串的方法

bcmp
bcopy
bzero
memccpy
memchr
memcmp
memcpy
memicmp
memmove
memset
movmem
setmem
stpcpy
strcat
strchr
strcmp
strcmpi
strcpy
strcspn
strdup
stricmp
strlen
strlwr
strncat
strncmp
strncmpi
strncpy
strnicmp
strpbrk
strrev
strset
strstr
strtok
strupr

11、字符分类

islower  小写字母
isdigit   十进制数
isupper  大写字母
isalnum  字母或数字
......

12、字符转换

#include <stdio.h>    
#include <string.h>    
  
int main(void)    
 {   
    printf("%c\n",tolower('A'));

 }  

输出:

jianguo@jianguo:/work/study$ sudo cc main.c -o main
jianguo@jianguo:/work/study$ ./main
a

还有其他的比如

toupper(int ch);等



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值