zoukankan      html  css  js  c++  java
  • 【总结】C语言中字符串的应用

    不能及时总结,就会习惯忘记。


    strcpy(char destination[], const char source[]);

    strcpy:将字符串source拷贝到字符串destination中。
    strcpy函数应用举例
    原型:strcpy(char destination[], const char source[]);
    功能:将字符串source拷贝到字符串destination中

    View Code
    #include <iostream.h>
    #include
    <string.h>
    void main(void)
    {
     
    char str1[10] = { "TsinghuaOK"};
     
    char str2[10] = { "Computer"};
      cout
    <<strcpy(str1,str2)<<endl;
    }

    //运行结果是:Computer

    //第二个字符串将覆盖掉第一个字符串的所有内容!

    //注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。所有字符串处理函数都包含在头文件string.h中。

    strncpy(char destination[], const char source[], int numchars);

    strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。
    strncpy函数应用举例
    原型:strncpy(char destination[], const char source[], int numchars);
    功能:将字符串source中前numchars个字符拷贝到字符串destination中

    例子:

    View Code
    1 #include <iostream.h>
    2 #include <string.h>
    3  void main(void)
    4 {
    5   char str1[10] = { "Tsinghua "};
    6   char str2[10] = { "Computer"};
    7   cout <<strncpy(str1,str2,3)<<endl;
    8 }
    9
    10  //运行结果:Comnghua
    11
    12  //注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!

    strcat(char target[], const char source[]);

    strcat:将字符串source接到字符串target的后面。
    strcat函数应用举例
    原型:strcat(char target[], const char source[]);
    功能:将字符串source接到字符串target的后面

    View Code
    1 #include <iostream.h>
    2 #include <string.h>
    3  void main(void)
    4 {
    5   char str1[] = { "Tsinghua "};
    6   char str2[] = { "Computer"};
    7   cout <<strcpy(str1,str2)<<endl;
    8 }
    9
    10  //运行结果:Tsinghua Computer
    11  
    12  /*
    13 注意:在定义字符数组1的长度时应该考虑字符数组2的长度,
    14 因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,
    15 字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。
    16  */

    strncat(char target[], const char source[], int numchars);

    strncat:将字符串source的前numchars个字符接到字符串target的后面。
    strncat函数应用举例:
    原型:strncat(char target[], const char source[], int numchars);
    功能:将字符串source的前numchars个字符接到字符串target的后面

    View Code
    1 #include <iostream.h>
    2 #include <string.h>
    3  void main(void)
    4 {
    5   char str1[] = { "Tsinghua "};
    6   char str2[] = { "Computer"};
    7   cout <<strncat(str1,str2,3)<<endl;
    8 }
    9
    10  //运行结果:Tsinghua Com

    int strcmp(const char firststring[], const char secondstring);

    strcmp:比较两个字符串firststring和secondstring。
    strcmp函数应用举例
    原型:int strcmp(const char firststring[], const char secondstring);
    功能:比较两个字符串firststring和secondstring

    View Code
    1 #include <iostream.h>
    2 #include <string.h>
    3  void main(void)
    4 {
    5   char buf1[] = "aaa";
    6   char buf2[] = "bbb";
    7   char buf3[] = "ccc";
    8   int ptr;
    9   ptr = strcmp(buf2,buf1);
    10   if(ptr > 0)
    11    cout <<"Buffer 2 is greater than buffer 1"<<endl;
    12   else
    13    cout <<"Buffer 2 is less than buffer 1"<<endl;
    14   ptr = strcmp(buf2,buf3);
    15   if(ptr > 0)
    16    cout <<"Buffer 2 is greater than buffer 3"<<endl;
    17   else
    18    cout <<"Buffer 2 is less than buffer 3"<<endl;
    19 }
    20
    21 /*
    22 运行结果是:
    23 Buffer 2 is less than buffer 1
    24 Buffer 2 is greater than buffer 3
    25 */

    strlen( const char string[] );

    strlen:统计字符串string中字符的个数。  
    strlen函数应用举例
    原型:strlen( const char string[] );
    功能:统计字符串string中字符的个数

    例子

    View Code
    1 #include <iostream.h>
    2 #include <string.h>
    3 void main(void)
    4 {
    5 char str[100];
    6 cout <<"请输入一个字符串:";
    7 cin >>str;
    8 cout <<"The length of the string is :"<<strlen(str)<<""<<endl;
    9 }
    10
    11 /*
    12 运行结果The length of the string is x (x为你输入的字符总数字)
    13
    14 注意:strlen函数的功能是计算字符串的实际长度,不包括'\0'在内。
    15 另外,strlen函数也可以直接测试字符串常量的长度,
    16 如:strlen("I Love cnblogs")。
    17 */
  • 相关阅读:
    Spring深入浅出(九),注解,@Autowired/@Resource,及其它(@Component/@Repository/@Service/@Controller)
    Spring深入浅出(八),自动装配,constructor
    使用node-webkit包装浏览器
    node-webkit 屏幕截图功能
    nodeJS 爬虫,通过Puppeteer实现滚动加载
    nodejs爬虫笔记(四)---利用nightmare解决加载更多问题
    一款开源免费的WPF图表控件ModernuiCharts
    使用各种 WPF Chart图表控件、插件的经历回顾与总结
    android 控件转bitmap
    一些关于 SAP Spartacus 组件和模块延迟加载的问题和解答
  • 原文地址:https://www.cnblogs.com/fanyong/p/1994415.html
Copyright © 2011-2022 走看看