[String processing function-part1]

1. Get the string length function

#include <string.h>
size_t strlen(const char *s);
  • Function: Calculate the length of a string
  • Parameters:
    s: the specified string
  • Return value:
    the length of the current string
  • Note: The string length acquired by strlen ends with the first \0 and \0 is not counted as the string length
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    // 使用strlen 函数获取字符串长度
    // strlenn获取的字符串的长度遇到第一个\0结束
    char s1[100] = "hel\0lo";

    printf("s1_len = %d\n", strlen(s1));
    printf("s1_size = %d\n", sizeof(s1));

    char *s2 = "hello";

    printf("s2_len = %d\n", strlen(s2));
    printf("s2_size = %d\n", sizeof(s2));

    return 0;
}

Results of the
Execution result 1

Second, the string copy function

#include <string.h>
char *strcpy(char *dest, const char *src);
  • Function: copy src to dest
  • parameter:
    • dest: destination string
    • src: source string
  • return value:
    • Save the first address of the dest string
  • Notice:
    • When using the strcpy function to copy strings, dest must be large enough, otherwise memory overflow will occur
    • strcpy is to copy the first \0 in the src string including \0 to dest
char *strncpy(char *dest, const char *src, size_t n);
  • Description of the function:
    • Copy the first n bytes of the string pointed to by src to the memory pointed to by dest
  • return value:
    • The first address of the destination memory
  • Notice:
    • strncpydo not copy ‘\0’;
    • If it nis greater than srcthe number of characters in the pointed-to string, it will destbe padded with n‐strlen(src)characters ’\0’.
      Results of the:
      Execution result strcpy

Third, the string append function

#include <string.h>
char *strcat(char *dest, const char *src);
  • Function: Append src to the end of dest
  • parameter:
    • dest: destination string
    • src: source string
  • return value:
    • Save the first address of the dest string
char *strncat(char *dest, const char *src, size_t n);
  • Function: Append the srcfirst character of the pointed string nto the end of the string pointed to by dest.
  • Note: If the number of characters is ngreater than , the string will only be appended to the end of the pointed string when appending .srcsrcdest’\0’
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    // 使用strcat函数追加字符串
    char s1[32] = "hello world";
    char s2[32] = "abcdefg";

    //strcat是从s1的\0的位置开始追加,直到s2的第一个\0复制完毕后结束
    strcat(s1,s2);

    printf("s1 = %s\n", s1);

    return 0;
}

Results of the:
Execution result strcat

Fourth, the string comparison function

#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
  • Function: strcmpto compare the contents of two strings, strncmpto compare whether the first n bytes of the two strings are the same
  • parameter:
    • s1, s2: two strings to compare
    • n: strncmpThe parameter n in represents the number of bytes to be compared
  • return value:
    • 0: s1 = s2
    • >0: s1 > s2
    • <0: s1 < s2
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    //使用strcmp比较两个字符串的内容是否一致
    //strcmp函数一个字符一个字符比较,只要出现不一样的,就会立即返回
    char s1[] = "hello";
    char s2[] = "a";

    int ret = strcmp(s1,s2);

    if(ret == 0)
    {
    
    
        printf("s1 = s2\n");
    }
    else if(ret > 0)
    {
    
    
        printf("s1 > s2\n");
    }
    else
    {
    
    
        printf("s1 < s2\n");
    }


    return 0;
}

Results of the:
Execution result strcmp

Five, character lookup function

#include <string.h>
char *strchr(const char *s, int c);
  • Function: Find the character whose ascii code is c in the string pointed to by the character pointer s
  • parameter:
    • s: the specified string
    • c: the character to look for
  • return value:
    • success: address of character found
    • Failed: NULL
  • Note: there are multiple characters whose ASCII is c in the string pointed to by s, then the first character is found
char *strrchr(const char *s, int c);
  • Function: In the string pointed to by s, find the last occurrence of the character whose ASCII is c.
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    //使用strchr函数在一个字符串中查找字符
    char s[] = "hel6lo wor6ld";

    //找第一个匹配的字符
    char *ret = strchr(s, '6');

    //找最后一个匹配的字符
    //char *ret = strrchr(s, '6');

    if(ret == NULL)
    {
    
    
        printf("not found\n");
    }
    else
    {
    
    
        printf("found, at the %dth position of string s\n", ret-s);
    }

    return 0;
}

Results of the:
Execution result strchr

6. String matching function

#include <string.h>
char *strtsr(const char *haystack, const char *needle);
  • Function description: Find the string pointed to by needle in the string pointed to by haystack, which is also the first match
  • return value:
    • Found: the first address of the found string
    • Not found: return NULL
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    //使用strstr函数在一个字符串中查找另一个字符串
    char s[] =  "1234:4567:666:789:666:7777";

    //strstr查找的时候,查找的是第二个参数的第一个\0之前的内容
    char *ret =  strstr(s, "666");

    if(ret == NULL)
    {
    
    
        printf("not found\n");
    }
    else
    {
    
    
        printf("found, at the %dth position of string s\n", ret - s);
    }

    return 0;
}

Results of the:Execution result strstr

Seven, string conversion value

#include <stdlib.h>
int atoi(const char *nptr);
  • Function: Convert a numeric string to integer data
  • parameter:
    • nptr: the specified string
  • return value:
    • The obtained shaping data
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    
    
    //使用atoi将数字型字符串转化为整形数据
    char s1[] = "7856";
    int ret1 = atoi(s1);

    printf("ret1 = %d\n",ret1);

    //使用atof将浮点型的字符串转化为浮点型数据
    char s2[] =  "3.1415926";
    double ret2 = atof(s2);

    printf("ret2 = %lf\n", ret2);

    return 0;
}

Results of the:
Execution result atoi

Guess you like

Origin blog.csdn.net/shuting7/article/details/130206272