字符函数和字符串函数

目录

1. 字符分类函数

2. 字符转换函数

3. strlen的使用和模拟实现

4. strcpy 的使用和模拟实现

5. strcat 的使用和模拟实现

6. strcmp 的使用和模拟实现

7. strncpy 函数的使用

8. strncat 函数的使用

9. strncmp函数的使用

10. strstr 的使用和模拟实现

11. strtok 函数的使用

12. strerror 函数的使用

13. perror函数的使用


1. 字符分类函数

头文件是ctype.h

 举例:

int islower ( int c );

islower 是能够判断参数部分的 c 是否是小写字母,通过返回值来说明是否是小写字母,如果是小写字母就返回非 0 的整数,如果不是小写字母,则返回 0

应用:

#include <stdio.h>
#include <ctype.h>

int main ()
{
    int i = 0;
    char str[] = "Test String.\n";
    char c;
    while (str[i])//到'\0'
    {
        c = str[i];
        if (islower(c))
            c -= 32;
       putchar(c);//打印
        i++;
    }
    return 0;
}

 

2. 字符转换函数
int tolower ( int c ); //将参数传进去的大写字母转小写
int toupper ( int c ); //将参数传进去的小写字母转大写

通过转换函数 tolower 实现小写转大写

#include <stdio.h>
#include <ctype.h>

int main ()
{
    int i = 0;
    char str[] = "Test String.\n";
    char c;
    while (str[i])
    {
        c = str[i];
        if (islower(c))
            c = toupper(c);
        putchar(c);
    i++;
    }
    return 0;
}

 

3. strlen的使用和模拟实现
size_t strlen ( const char * str );
  • 字符串以'\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含'\0' )
  • 参数指向的字符串必须要以'\0' 结束
  • 注意函数的返回值为size_t,是无符号的( 易错)

应用:

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

int main()
{
    const char* str1 = "abcdef";
    const char* str2 = "bbb";
    if(strlen(str2)-strlen(str1)>0)
    {
        printf("str2>str1\n");
    }
    else
    {
        printf("srt1>str2\n");
    }
    return 0;
}

模拟实现:

#include<stdio.h>
#include<assert.h>

//方式1:
//计数器方式
int my_strlen(const char * str)
{
    int count = 0;
    assert(str);//断言
    while(*str)
    {
    count++;
    str++;
    }
    return count;
}

//方式2:
//不能创建临时变量计数器
int my_strlen(const char * str)
{
    assert(str);
    if(*str == '\0')
        return 0;
    else
        return 1+my_strlen(str+1);//递归
}

//方式3:
//指针-指针的方式
int my_strlen(char *s)
{
    assert(str);
    char *p = s;
    while(*p != ‘\0’ )
        p++;
    return p-s;//指针-指针为元素个数
}

4. strcpy 的使用和模拟实现
 
char* strcpy(char * destination, const char * source );

Copies the C string pointed by source into the array pointed by destination, including the
terminating null character (and stopping at that point).

  • 源字符串必须以'\0' 结束
  • 将源字符串中的'\0' 拷贝到目标空间
  • 目标空间必须确保能存放源字符串
  • 目标空间必须可修改

模拟实现:

char *my_strcpy(char *dest, const char*src)
{
    char *ret = dest;
    assert(dest != NULL);
    assert(src != NULL);
    while((*dest++ = *src++))//使用后变量++  当*src=='\0'时,*dest = *src -> n = 0 ->while(假)
    {
        ;//必须有 ;    空语句
    }
    return ret;
}

 

5. strcat 的使用和模拟实现

Appends a copy of the source string to the destination string. The terminating null character
in destination is overwritten by the first character of source, and a null-character is included
at the end of the new string formed by the concatenation of both in destination.

  • 源字符串必须以'\0' 结束
  • 目标字符串中也得有\0 ,否则不知道从哪里开始追加
  • 目标空间必须能容纳下源字符串的内容
  • 目标空间必须可修改

模拟实现

char *my_strcat(char *dest, const char*src)
{
    char *ret = dest;
    assert(dest != NULL);
    assert(src != NULL);
    while(*dest)//找到'\0'
    {
        dest++;
    }
    while((*dest++ = *src++))
    {
        ;
    }
     return ret;
}

 

6. strcmp 的使用和模拟实现

This function starts comparing the first character of each string. If they are equal to each
other, it continues with the following pairs until the characters differ or until a terminating
null-character is reached.

  • 第一个字符串大于第二个字符串则返回大于0的数字
  • 第一个字符串等于第二个字符串则返回0
  • 第一个字符串小于第二个字符串则返回小于0的数字
int my_strcmp (const char * str1, const char * str2)
{
    int ret = 0 ;
    assert(src != NULL);//断言
    assert(dest != NULL);
    while(*str1 == *str2)
    {
        if(*str1 == '\0')
            return 0;
        str1++;
        str2++;
    }
    return *str1-*str2;
}

 

7. strncpy 函数的使用
char * strncpy ( char * destination, const char * source, size_t num );

Copies the first num characters of source to destination. If the end of the source C string
(which is signaled by a null-character) is found before num characters have been copied,
destination is padded with zeros until a total of num characters have been written to it.

  • 拷贝num个字符从源字符串到目标空间
  • 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加'\0',直到num个

 

8. strncat 函数的使用
char * strncat ( char * destination, const char * source, size_t num );
  • Appends the first num characters of source to destination, plus a terminating null-character.(将source指向字符串的前num个字符追加到destination指向的字符串末尾,追加 '\0' 字符)
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.(如果source 指向的字符串的长度小于num的时候,只会将字符串中到' \0' 的内容追加到destination指向的字符串末尾)
/* strncat example */
#include <stdio.h>
#include <string.h>
int main ()
{
    char str1[20];
    char str2[20];
    strcpy (str1,"To be ");
    strcpy (str2,"or not to be");
    strncat (str1, str2, 6);
    printf("%s\n", str1);//To be or not
    return 0;
}

 

9. strncmp函数的使用
int strncmp ( const char * str1, const char * str2, size_t num );

比较str1和str2的前num个字符,如果相等就继续往后比较,最多比较num个字母,如果提前发现不一样就提前结束,大的字符所在的字符串大于另外一个,如果num个字符都相等,就是相等返回0

 

10. strstr 的使用和模拟实现
char * strstr ( const char * str1, const char * str2);
  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.(函数返回字符串str2在字符串str1中第一次出现的位置)
  • The matching process does not include the terminating null-characters, but it stops there.(字符串的比较匹配不包含 '\0' 字符,以 '\0' 作为结束标志)
/* strstr example */
#include <stdio.h>
#include <string.h>
int main ()
{
    char str[] ="This is a simple string";
    char * pch;
    pch = strstr (str,"simple");
    strncpy (pch,"sample",6);
    printf("%s\n", str);//This is a sample string    sample/simple
    return 0;
}

模拟实现:

char * strstr (const char * str1, const char * str2)
{
    char *cp = (char *) str1;
    char *s1, *s2;
    if ( !*str2 )
        return((char *)str1);
    while (*cp)
    {
        s1 = cp;
        s2 = (char *) str2;
        while ( *s1 && *s2 && !(*s1-*s2) )//*s1 && *s2 防止s1==s2
        s1++, s2++;
        if (!*s2)
            return(cp);
        cp++;
    }
    return(NULL);
}

11. strtok 函数的使用
char * strtok ( char * str, const char * sep);
  • sep参数指向一个字符串,定义了用作分隔符的字符集合
  • 第一个参数指定一个字符串,它包含了0个或者多个分隔符(由sep字符串中一个或者多个分隔符)
  • strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:
  • strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改)
  • strtok函数的第一个参数不为NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置
  • strtok函数的第一个参数为NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记
  • 如果字符串中不存在更多的标记,则返回NULL 指针
#include <stdio.h>
#include <string.h>
int main()
{
    char arr[] = "192.168.6.111";
    char* sep = ".";
    char* str = NULL;
    for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
    {
        printf("%s\n", str);
    }
    return 0;
}

 

12. strerror 函数的使用
char * strerror ( int errnum );

strerror函数可以把参数部分错误码对应的错误信息的字符串地址返回来 一般是放在 errno.h 中

C语言程序启动的时候就会使用一个全局的变量errno来记录程序的当前错误码,只不过程序启动
的时候errno是0,表示没有错误,当我们在使用标准库中的函数的时候发生了某种错误,就会将对应的错误码,存放在errno中

#include <errno.h>
#include <string.h>
#include <stdio.h>
//我们打印一下0~10这些错误码对应的信息
int main()
{
    int i = 0;
    for (i = 0; i <= 10; i++) 
    {
        printf("%s\n", strerror(i));
    }
    return 0;
}
//Windows11+VS2022环境下输出的结果
1  No error
2  Operation not permitted
3  No such file or directory
4  No such process
5  Interrupted function call
6  Input/output error
7  No such device or address
8  Arg list too long
9  Exec format error
10 Bad file descriptor
11 No child processes

举例:

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
    FILE * pFile;
    pFile = fopen ("unexist.ent","r");//打开文件失败
    if (pFile == NULL)
        printf ("Error opening file unexist.ent: %s\n", strerror(errno));
//Error opening file unexist.ent: No such file or directory
    return 0;
}

 

13. perror函数的使用

perror函数打印完参数部分的字符串后,再打印一个冒号和一个空格,再打印错误信息

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main ()
{
    FILE * pFile;
    pFile = fopen ("unexist.ent","r");//打开文件失败
    if (pFile == NULL)
        perror("Error opening file unexist.ent");
//Error opening file unexist.ent: No such file or directory
    return 0;
}

谢谢观看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值