zoukankan      html  css  js  c++  java
  • 常用的字符串函数的几种操作

    声明 a是destination的首地址,b是source的地址

    (1)strcpy
    作用:strcpy的作用是把source的b复制到destination中的a
    使用方式:strcpy(a,b)
    使用示例程序如下:

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

    int main(void)
    {
    char array_test[10] = {0};
    char *dest_str = "hello!";
    strcpy(array_test,dest_str);

    printf("array_test = %s. ",array_test);

    return 0;
    }
    打印结果:array_test = hello!

    (2)strncpy
    作用:strncpy的作用是把source的b中的n个字符复制到destination中的b
    使用方式:strcpy(a,b,n)
    使用示例程序如下:
    #include<stdio.h>
    #include<string.h>

    int main(void)
    {
    char array_test[10] = {0};
    char *dest_str = "hello!";
    strncpy(array_test,dest_str,2);

    printf("array_test = %s. ",array_test);

    return 0;
    }

    打印结果:array_test = he.
    (3)strcmp
    作用:strcmp(a,b)的作用是把a和b进行比对,如果a==b则返回一个等于0的值,如果a>b则返回一个大于0的值,如果a<b则返回一个小于0的值。
    使用方法:strcmp(a,b)
    使用示例程序:
    #include<stdio.h>
    #include<string.h>

    int main(void)
    {


    char *a = "hELLO!";
    char *b = "Hello!";

    int value = strcmp(a,b);
    if(value == 0)
    printf("a==b. ");
    else if(value < 0)
    printf("a>b. ");
    else if(value > 0)
    printf("a<b. ");
    else
    {
    printf("wrong. ");
    }

    return 0;
    }

    打印结果:a<b.

    (4)strlen
    作用:计算字符串的长度
    使用方法:strlen(b)
    使用示例程序:
    #include<stdio.h>
    #include<string.h>

    int main(void)
    {

    char *b = "afjoaf";
    int value = strlen(b);

    printf("value = %d. ",value);

    return 0;
    }

    打印结果:value = 6.

  • 相关阅读:
    触发器
    自定义变量
    系统变量
    Interval 计时器
    Ajax 之 DWR
    cssTest
    Ajax之XMLHttpRequst对象
    添加页面元素
    jquery 基础
    jQuery 自定义动画效果
  • 原文地址:https://www.cnblogs.com/xing-ting/p/9785118.html
Copyright © 2011-2022 走看看