C字符串函数(一)

C字符串函数(一)

  • strlen() -> 统计字符串长度
  • strcat()strncat()函数 -> 拼接字符串函数
  • strcmp()strncmp()函数 -> 字符串比较函数
  • strcpy()strncpy()函数 -> 字符串复制函数

strlen()

本质上就是在处理字符数组.

示例代码:

# include<stdio.h>
# include<string.h> /* 字符串函数原型 */
void fit(char *string, unsigned int size);

int main(void)
{
    char mesg [] = "Things should be as simple as possible,"
                    "but not simpler.";
    puts(mesg);
    fit(mesg, 38); // 38位字符位置处截断
    puts(mesg);
    puts("Let's look at some more of the string.");
    puts(mesg + 39); // mesg + 39位置处字符开始往后输出

    getchar();

    return 0;
}

void fit(char *string, unsigned int size)
{
    /**
     * 本质上就是处理字符数组
     * 字符数组与字符串的区别在于字符串的字符数组最后一位是空字符串 -> '\0'
     * 字符数组使用数组名称作为指向数组第一个元素的指针 -> 头指针
    */
   if (strlen(string) > size)
    string[size] = '\0';
}

strcat()

作用:

  • 拼接字符串
  • 参数为两个字符串,把第二个字符串的备份附加在第一个字符串的末尾
  • 使用指针进行操作,并返回第一个字符串的指针

示例代码:

# include<stdio.h>
# include<string.h>
# define SIZE 80
char * s_gets(char * st, int n); // 返回值是指针

int main(void)
{
    char flower[SIZE];
    char addon [] = "s smell like old shoes.";

    puts("What is your favoritye flower?");
    if (s_gets(flower, SIZE))
    {
        strcat(flower, addon);
        puts(flower);
        puts(addon);
    }
    else
        puts("End of file encountered!");
    puts("Eyes");

    getchar();

    return 0;
    
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    int i = 0;

    ret_val = fgets(st, n, stdin); // 返回指向char 的指针,如果读到文件结尾,返回空指针
    if (ret_val)
    {
        while (st[i] != '\n' && st[i] != '\0')
            i++;
        if (st[i] == '\n')
            st[i] == '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

strncat()

strcat()函数存在的问题:

  • 无法检测第一个字符串是否能容纳第二个人字符串
  • 如果第一个字符串不够大就会出现溢出问题

strncat()函数参数:

  • 字符串A
  • 字符串B
  • 最大添加的字符数量

示例代码:

# include<stdio.h>
# include<string.h>
# define SIZE 30
# define BUGSIZE 13
char * s_gets(char * st, int n);

int main(void)
{
    char flower[SIZE];
    char addon [] = "s smell like old shoes.";
    char bug[BUGSIZE];
    int available;

    puts("What is your favorite flower?");
    s_gets(flower, SIZE);
    
    if ((strlen(addon) + strlen(flower) + 1) <= SIZE) // 判断可以容纳
        strcat(flower, addon);
    
    puts(flower);
    puts("What is your favorite bug?");
    s_gets(bug, BUGSIZE);
    available = BUGSIZE - strlen(bug) - 1;
    strncat(bug, addon, available); // 安全拼接
    puts(bug);

    getchar();

    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    int i = 0;

    ret_val = fgets(st, n, stdin); // 返回指向char 的指针,如果读到文件结尾,返回空指针
    if (ret_val)
    {
        while (st[i] != '\n' && st[i] != '\0')
            i++;
        if (st[i] == '\n')
            st[i] == '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

strcmp()

作用:

  • 字符串比较 -> 比较的是字符串而不是指针

错误示例代码:

# include<stdio.h>
# define ANSWER "Grant"
# define SIZE 40

int main(void)
{
    char try[SIZE];
    
    while(try != ANSWER)
    {
        // 这是错误的比较,因为这两个都变量都是指针,指针必定不相同.需要比较的是指针指向的字符串的值
    }
}

正确示例代码:

  • 借助字符串比较函数strcmp()实现
# include<stdio.h>
# define ANSWER "Grant"
# define SIZE 40

int main(void)
{
    char try[SIZE];
    
    while(strcmp(try, ANSWER) != 0)
    {
        
    }
}

strcmp()函数的比较方式:

  • 机器排序序列进行比较 -> 每一个字符逐位进行比较
  • 比较方式是使用第一个字符剪第二个字符结果是正数、0、负数 -> 以此来观察是否是不同的字符 -> 只有0表示相同的字符,其他都表示不同的字符
  • 出现负数的原因是比较方法是使用第一个字符的ASCII码剪掉第二个字符的ASCII

使用strcmp()检测程序是否需要停止读取输入(示例代码):

# include<stdio.h>
# include<string.h>
# define SIZE 80
# define LIM 10
# define STOP "quit"
char * s_gets(char * st, int n);

int main(void)
{
    char input[LIM][SIZE];
    int ct = 0;

    printf("Enter up to %d lines (type quit to quit):\n", LIM);
    while (ct < LIM && s_gets(input[ct], SIZE) != NULL && strcmp(input[ct], STOP) != 0)
    {
        ct++;
    }
    printf("%d strings enterd\n", ct);

    getchar();

    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    int i = 0;

    ret_val = fgets(st, n, stdin); // 返回指向char 的指针,如果读到文件结尾,返回空指针
    if (ret_val)
    {
        while (st[i] != '\n' && st[i] != '\0')
            i++;
        if (st[i] == '\n')
            st[i] == '\0';
        else
            while (getchar() != '\n')
                continue;
    }

    return ret_val;
}

strncmp()

作用:

  • strcmp一样,但是可以限定查找的字符数

示例代码:

# include<stdio.h>
# include<string.h>
# define LISTSIZE 6

int main(void)
{
    const char * list[LISTSIZE] = 
    {
        "astronomy", "astounding", "astrophtsics", "ostracize", "asterism", "astrophobia"
    };

    int count = 0;
    int i;

    for ( i = 0; i < LISTSIZE; i++)
    {
        if (strncmp(list[i], "astro", 5) == 0) // 只查找前五个字符相同的字符串
        {
            printf("Found: %s \n", list[i]);
            count++;
        }  
    }

    printf("The list contained %d words begining with astro.\n", count);
    
    getchar();

    return 0;

}
posted @   俊king  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示