• 【C】常用的字符串函数


    1. strcpy

      函数名:strcpy

      用法:char *strcpy(char *destin, char *cource)

      功能:将一个字符串从一个拷贝到另外一个

      程序示例:

      

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(){
     5   char str1[] = "source";
     6   char str2[] = "des";
     7 
     8   strcpy(str1,str2);
     9   printf("str1 : %s
    ",str1);
    10   return 0;
    11 }

      程序输出:

      

    2. strnpy

      函数名:strnpy

      用法:char * strncpy(char *dest, char *src,size_t n);

      功能:将字符串src中的前n个字符复制到字符串数组dest中,注意(不会清除dest数组中原来的数据,只是将新的数据覆盖)

      程序示例:

      

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(){
     5   char str1[] = "source";
     6   char str2[] = "dest";
     7 
     8   strncpy(str1,str2,4);
     9   printf("str1 : %s
    ",str1);
    10   return 0;
    11 }

      程序结果:(注意,函数没有清理原数组)

      

    3.strcat

      函数名:strcat

      用法: char *strcat(char *destin, char *source)

      功能:将source 拼接到 destin 字符串后

      程序示例

      

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(){
     5   char str1[] = "source";
     6   char str2[] = "dest";
     7 
     8   // strcpy(str1,str2);
     9   strcat(str1,str2);
    10   printf("str1 : %s
    ",str1);
    11   return 0;
    12 }

      程序输出

      

    4. strchr

      函数名:strchr

      用法:char *strchr(char *str, char *c);

      功能:在str 字符串中查找字符(串)c 得匹配之处,返回该指针,如果没有返回NULL

      程序实例:

      

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main(){
     5   char str1[] = "source";
     6   char str2 = 'c';
     7 
     8   // strcpy(str1,str2);
     9   char *strFind = strchr(str1,str2);
    10   printf("strFind : %c
    ",*strFind);
    11   return 0;
    12 }

      程序结果:

      

      

  • 相关阅读:
    函数中的不定长参数研究 *and**
    copy()与直接=赋值的区别
    python的位运算符
    python的list相关知识
    hadoop报错:hdfs.DFSClient: Exception in createBlockOutputStream
    转载计算机的潜意识的文章:机器学习的入门级经典读物
    完全分布式hadoop2.5.0安装 VMware下虚拟机centos6.4安装1主两从hadoop
    PE结构总导航
    利用函数指针绕过断点
    为什么Fun函数能够执行
  • 原文地址:https://www.cnblogs.com/zleiblogs/p/7214490.html
Copyright © 2020-2023  润新知