c语言 字符串操作函数

字符串操作函数

1.strlen()函数

strlen()函数用于计算字符串的长度,返回字符串的字符数。

语法:

size_t strlen(const char *str)

参数:

  • str – 要计算长度的字符串。

返回值:

  • 字符串的字符数。

示例:

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

int main ()
{
   char str[50]="hello world";
   len = strlen(str);
   printf("|%s| 的长度是 |%d|\n", str, len);
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果:

|hello world| 的长度是 |11|

长度为11,因为它包含了11个字符。

自实现

int myStrlen(const char * str)
{
    int len = 0;
    while(*str++)
    len++;
    return len;
}

2.strcpy()函数

strcpy()函数用于复制一个字符串到另一个字符串。

语法:

char *strcpy(char *dest, const char *src)

参数:

  • dest – 目标字符串。
  • src – 源字符串。

返回值:

  • 目标字符串。

示例:

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

int main ()
{  char str1[50];
   char str2[50]="hello";
   strcpy(str1, str2);
   printf("|%s| 复制到 |%s|\n", str1, str2);
   return 0;
}

输出:

|hello| 复制到 |hello|

自实现

char *myStrcpy(char *dst, const char * src)
{
    char *d = dst;
    while(*dst++ = *src++);
    return d;
}

3.strncpy()函数

函数 char *strncpy(char *dest, const char *src, size_t n)
把 src 所指向的字符串复制到 dest,最多复制 n 个字符。
当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充。

语法:

char *strncpy(char *dest, const char *src, size_t n)

参数:

  • dest – 目标字符串。
  • src – 源字符串。
  • n – 要复制的字符数。

返回值:

  • 目标字符串。
  • 如果 n 为 0,则返回 NULL。

示例:

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

int main ()
{  char str1[50];
   char str2[50]="hello world";
   strncpy(str1, str2, 5);
   printf("|%s| --- |%s|\n", str1, str2);
   return 0;
}

输出:

|hello| --- |hello world|

自实现

char *myStrncpy(char *dst, const char *src, int n)
{
    char *d = dst;
    while(n-- && (*dst++ = *src++));
    while(n-- > 0)
        *dst++ = '\0';
    return d;
}

4.strcat()函数

函数 char *strcat(char *dest, const char *src)
把 src 所指向的字符串追加到 dest 后面。

语法:

char *strcat(char *dest, const char *src)

参数:

  • dest – 目标字符串。
  • src – 源字符串。

返回值:

  • 目标字符串。

示例:

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

int main ()
{  char str1[50]="hello ";
   char str2[50]="world";
   strcat(str1, str2);
   printf("|%s|\n", str1);
   return 0;
}

输出:

|hello world|

自实现

char *myStrcat(char *dst, const char *src)
{
    char *d = dst;
    while(*d)
        d++;
    while((*d++ = *src++));
    return dst;
}

5.strncat()函数

函数 char *strncat(char *dest, const char *src, size_t n)
把 src 所指向的字符串追加到 dest 后面,最多追加 n 个字符。

语法:

char *strncat(char *dest, const char *src, size_t n)

参数:

  • dest – 目标字符串。
  • src – 源字符串。
  • n – 要追加的字符数。

返回值:

  • 目标字符串。

示例:

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

int main ()
{  char str1[50]="hello ";
   char str2[50]="world";
   strncat(str1, str2, 5);
   printf("|%s|\n", str1);
   return 0;
}

输出:

|hello world|

自实现

char *myStrcat(char *dst, const char *src)
{
    char * d = dst;
    while(*dst) dst++;
    while(*dst++ = *src++);
    return d;
}

6.strcmp()函数

函数 int strcmp(const char *str1, const char *str2)
比较两个字符串的大小。

语法:

int strcmp(const char *str1, const char *str2)

参数:

  • str1 – 字符串1。
  • str2 – 字符串2。
  • 返回值:
  • 如果 str1 等于 str2,则返回 0。
  • 如果 str1 小于 str2,则返回小于 0 的值。
  • 如果 str1 大于 str2,则返回大于 0 的值。

示例:

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


int main ()
{  char str1[50]="hello";
   char str2[50]="world";
   int result;
    
   result = strcmp(str1, str2);
   if (result < 0)
      printf("|%s| 小于 |%s|\n", str1, str2);
   else if (result > 0)
      printf("|%s| 大于 |%s|\n", str1, str2);
   else
      printf("|%s| 等于 |%s|\n", str1, str2);


   return 0;


}

输出:

|hello| 小于 |world|

自实现

int myStrcmp(const char * s1, const char * s2)
{
    for(;*s1 && *s2; s1++,s2++) {
        if(*s1 == *s2)
            continue;
        else
        return *s1 -*s2;
    }
    return *s1 -*s2;
}   

7.strncmp()函数

函数 int strncmp(const char *str1, const char *str2, size_t n)
比较两个字符串的前 n 个字符的大小。

语法:

int strncmp(const char *str1, const char *str2, size_t n)

参数:

  • str1 – 字符串1。
  • str2 – 字符串2。
  • n – 要比较的字符数。
  • 返回值:
  • 如果 str1 等于 str2,则返回 0。
  • 如果 str1 小于 str2,则返回小于 0 的值。
  • 如果 str1 大于 str2,则返回大于 0 的值。

示例:

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


int main ()
{  char str1[50]="hello";
   char str2[50]="world";
   int result;
    
   result = strncmp(str1, str2, 4);
   if (result < 0)
      printf("|%s| 小于 |%s|\n", str1, str2);
   else if (result > 0)
      printf("|%s| 大于 |%s|\n", str1, str2);
   else
      printf("|%s| 等于 |%s|\n", str1, str2);


   return 0;


}

输出:

|hello| 小于 |world|

自实现

int myStrncmp(const char * s1, const char * s2, int n)
{
    for(;*s1 && *s2 && n; s1++,s2++,n--) {
        if(*s1 == *s2)
            continue;
        else
        return *s1 -*s2;
    }
   return 0;
}   

8.sprintf()函数

函数 int sprintf(char *str, const char *format, …)
把格式化的数据写入某个字符串缓冲区

语法:

int sprintf(char *str, const char *format, ...)

参数:

  • str – 字符串缓冲区。
  • format – 格式字符串。
  • … – 可变参数。

返回值:

  • 写入的字符数。

示例:

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


int main()
{
    int a,b,c,d;
    printf("pls input ip:");
    scanf("%d.%d.%d.%d",&a,&b,&c,&d);
    char buf[16];
    sprintf(buf,"%d.%d.%d.%d",a,b,c,d);
    printf("%s\n",buf);
    return 0;
}

输出:

pls input ip:124.13.3.5
124.13.3.5

自实现

不会

##9.atoi()函数

函数 int atoi(const char *nptr)
将字符串转换成整型数;atoi()会扫描参数 nptr 字符串,跳过前面的空格字
符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符(‘\0’)
时才结束转化,并将结果返回(返回转换后的整型数)。

语法:

int atoi(const char *nptr)

参数:

  • str – 字符串。

返回值:

  • 转换后的整数。

示例:

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

       
int main()
{
    char str[100] = "12345";        
    
    int num = atoi(str);
    
    printf("The number is: %d\n", num);
    
    return 0;
}

输出:

The number is: 12345

自实现

不会

10.itoa()函数

函数 itoa
根据指定的进制(base),将整型数据转化为以’\0’结尾的字符串,保
存到 str 指向的字符数组中。

语法:

char * itoa ( int value, char * str, int base );(非标库函数)

参数:

  • value – 待转化的整数。
  • str – 存放字符串的空间。
  • radix – 进制。

返回值:

  • 转化后的字符串首地址。

示例:

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


int main()
{
    int num = 12345;
    char str[100];

    itoa(num, str, 10);
    printf("The string is: %s\n", str);
    itoa(num, str, 8);
    printf("The string is: %s\n", str);
    itoa(num, str, 5);
    printf("The string is: %s\n", str);
    itoa(num, str, 3);
    printf("The string is: %s\n", str);
    itoa(num, str, 2);
    printf("The string is: %s\n", str);
    itoa(num, str, 36);
    printf("The string is: %s\n", str);
    return 0;
}



输出:

The string is: 12345
The string is: 30071
The string is: 343340
The string is: 121221020
The string is: 11000000111001
The string is: 9ix

自实现

🤨

11.strchr()函数

char strchr(char str,int ch)
返回字符串 str 中首次出现字符 c 的位置指针,找不到返回 NULL。

语法:



char *strchr(char* str,int ch)

参数:

  • str – 字符串。
  • ch – 要查找的字符。

返回值:

  • 字符串中首次出现字符 c 的位置指针,找不到返回 NULL。

示例:

#include <stdio.h>
#include <string.h>
int main()
{
    char buf[100] = "china";
    char *p = strchr(buf,'n');
    printf("%s\n",p);
    return 0;
}

输出:

na

统计一个字符在字符串内出现的次数


int calcCharCountOfString(char * str, char ch)
{
    int count = 0;
    while(*str != '\0')
    {
        if(ch == *str)
        count++;
        str++;
    }
    return count;
}
int calcCharCountOfString(char * str, char ch)
{
    int count = 0;
    while(str = strchr(str,ch))
    {
        count++;
        str++;
    }
    return count;
}

自实现:

char * myStrchr(char *str, int ch)
{
    while(*str != ch&& *str != '\0')
        str++;
    if(*str == '\0')
        return NULL;
    else
        return str;
}

12.strspn()函数

函数 int strspn(const char *s, const char *accept)
返回字符串 s 中,从左到右,第一个不在 accept 字符串中的字符的位置。

语法:

int strspn(const char *s, const char *accept)

参数:

  • s – 字符串。
  • accept – 要接受的字符集。

返回值:

  • 字符串 s 中,从左到右,第一个不在 accept 字符串中的字符的位置。

示例:

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100] = "hello world";
    char accept[100] = "hel";
    int len = strspn(str, accept);
    printf("%d\n", len);
    return 0;
}

输出:

4

自实现:


13.strcspn()函数

函数 int strcspn(const char *s, const char *reject)
返回字符串 s 中,从左到右,第一个在 reject 字符串中的字符的位置。

语法:

int strcspn(const char *s, const char *reject)

参数:

  • s – 字符串。
  • reject – 要拒绝的字符集。

返回值:

  • 字符串 s 中,从左到右,第一个在 reject 字符串中的字符的位置。

示例:

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100] = "hello world";
    char reject[100] = "r";
    int len = strcspn(str, reject);
    printf("%d\n", len);
    return 0;
}

输出:

8

自实现:


14.strstr()函数

函数 char * strstr ( char * str1, const char* str2 );
函数搜索字符串 str2 在字符串 str1 中是否出现。找到所搜
索的字符串,则该函数返回第一次匹配的字符串的地址;如果未找到所
搜索的字符串,则返回 NULL。

语法:

char * strstr ( char * str1, const char* str2 );

参数:

  • str1 – 被搜索的字符串。
  • str2 – 要搜索的字符串。

返回值:

  • 找到所搜索的字符串,则该函数返回第一次匹配的字符串的地址;如果未找到所搜索的字符串,则返回 NULL。

示例:

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[] = "abcd123456efg";
    char str2[] = "1234";
    char *pf = strstr(str1,str2);
    if(pf != NULL)
        printf("%s\n",pf);
    else
        printf("find none\n");
    return 0;
}

输出:

123456efg

自实现:

#include <stdio.h>
#include <string.h>
char * myStrstr(char *s1,char *s2)
{
    int n = strlen(s2);
    for(;(s1 = strchr(s1,*s2))!=NULL;s1++)
    {
        if(strncmp(s1,s2,n)==0)
        return s1;
    }
    return NULL;
}

统计一个字符串在另一个字符串内出现的次数

int calcStrCountOfString(char * string, char* str)
{
    int count = 0;
    int len = strlen(str);
    while(string = strstr(string,str))
    {
        count++;
        string += len;
    }
    return count;
}

15.strtok()函数

函数 char *strtok(char *s, char *delim);
函数用来分割字符串,并返回分割后的字符串。
分解字符串为一组字符串。s 为要分解的字符串,delim 为分隔符字符
串。首次调用时,s 指向要分解的字符串,之后再次调用要把 s 设成 NULL。
strtok 在 s 中查找包含在 delim 中的字符并用 NULL(‘\0’)来替换,直
到找遍整个字符串。

语法:

char *strtok(char *s, char *delim);

参数:

  • s – 要分解的字符串。
  • delim – 分隔符字符串。

返回值:

  • 分割后的字符串。

示例:

#include <stdio.h>
#include <string.h>
int main()
{
    char buf[] = "aaaaaaaaaa@bbbbbbbbbbbb@ccccccccccc@ddddddddd";
    char delim[] = "@"; // "@#"
    int size = sizeof(buf);
    for(int i=0; i<size; i++)
        printf("%c",buf[i]);
    putchar(10);
    strtok(buf,delim);
    for(int i=0; i<size; i++)
        printf("%c",buf[i]);
    putchar(10);
    strtok(NULL,delim); // strtok(buf,delim);
    for(int i=0; i<size; i++)
        printf("%c",buf[i]);
    putchar(10);
    strtok(NULL,delim); // strtok(buf,delim);
    for(int i=0; i<size; i++)
        printf("%c",buf[i]);
    putchar(10);
//第一个或是最后一个是分隔符,如何
    return 0;
}

输出:

aaaaaaaaaa@bbbbbbbbbbbb@ccccccccccc@ddddddddd
aaaaaaaaaa bbbbbbbbbbbb@ccccccccccc@ddddddddd
aaaaaaaaaa bbbbbbbbbbbb ccccccccccc@ddddddddd
aaaaaaaaaa bbbbbbbbbbbb ccccccccccc ddddddddd

使用示例

解析 linux 密码文件

#cat /etc/passwd
root:x:0:0:Superuser:/:
daemon:x:1:1:Systemdaemons:/etc:
bin:x:2:2:Ownerofsystemcommands:/bin:
sys:x:3:3:Ownerofsystemfiles:/usr/sys:
#include <stdio.h>
#include <string.h>
int main()
{


        char buf[] = "sys:x:3:3:Ownerofsystemfiles:/usr/sys:";
        char *p = strtok(buf,":");
        while(p != NULL)
        {
            printf("%s\n",p);
            p = strtok(NULL,":");
        }
        return 0;

}

输出:

sys
x
3
3
Ownerofsystemfiles
/usr/sys
  • 50
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值