【文字列処理機能-その1】

1. 文字列長関数を取得する

#include <string.h>
size_t strlen(const char *s);
  • 機能: 文字列の長さを計算します。
  • パラメータ:
    s: 指定された文字列
  • 戻り値:
    現在の文字列の長さ
  • 注: strlen で取得される文字列長は最初の \0 で終わり、\0 は文字列長としてカウントされません。
#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;
}

の結果
実行結果1

2番目に、文字列コピー関数

#include <string.h>
char *strcpy(char *dest, const char *src);
  • 機能: srcをdestにコピー
  • パラメータ:
    • dest: 宛先文字列
    • src: ソース文字列
  • 戻り値:
    • dest文字列の先頭アドレスを保存します
  • 知らせ:
    • strcpy 関数を使用して文字列をコピーする場合、dest は十分な大きさでなければなりません。そうしないとメモリ オーバーフローが発生します。
    • strcpy は、\0 を含む src 文字列の最初の \0 を dest にコピーします。
char *strncpy(char *dest, const char *src, size_t n);
  • 機能の説明:
    • src が指す文字列の最初の n バイトを dest が指すメモリにコピーします。
  • 戻り値:
    • 宛先メモリの先頭アドレス
  • 知らせ:
    • strncpyコピーしないでください‘\0’
    • 指定された文字列の文字数nより大きい場合は、文字が埋め込まれます結果:srcdestn‐strlen(src)’\0’

      実行結果 strcpy

第三に、文字列追加関数

#include <string.h>
char *strcat(char *dest, const char *src);
  • 機能: dest の最後に src を追加します。
  • パラメータ:
    • dest: 宛先文字列
    • src: ソース文字列
  • 戻り値:
    • dest文字列の先頭アドレスを保存します
char *strncat(char *dest, const char *src, size_t n);
  • 機能:src指定された文字列の最初の文字を、ndest が指定した文字列の末尾に追加します。
  • 注:文字数がnより大きい場合、文字列は を追加するときに指定された文字列の末尾にのみ追加されます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;
}

結果:
実行結果 strcat

4番目、文字列比較関数

#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
  • 機能: strcmp2 つの文字列の内容を比較し、strncmp2 つの文字列の最初の n バイトが同じかどうかを比較します。
  • パラメータ:
    • s1、s2: 比較する 2 つの文字列
    • n:strncmpパラメータ n は、比較されるバイト数を表します。
  • 戻り値:
    • 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;
}

結果:
実行結果 strcmp

五、文字検索機能

#include <string.h>
char *strchr(const char *s, int c);
  • 機能: 文字ポインタ s が指す文字列内で、ASCII コードが c である文字を検索します。
  • パラメータ:
    • s: 指定された文字列
    • c: 探す文字
  • 戻り値:
    • 成功: 文字のアドレスが見つかりました
    • 失敗: NULL
  • 注: s が指す文字列内に ASCII が c である文字が複数ある場合、最初の文字が検索されます。
char *strrchr(const char *s, int c);
  • 機能: s が指す文字列内で、ASCII が 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;
}

結果:
実行結果 strchr

6. 文字列マッチング機能

#include <string.h>
char *strtsr(const char *haystack, const char *needle);
  • 関数の説明: haystack が指す文字列内で、needle が指す文字列を検索します。これは最初の一致でもあります。
  • 戻り値:
    • Found: 見つかった文字列の最初のアドレス
    • 見つかりません: 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;
}

結果:実行結果 strstr

7、文字列変換値

#include <stdlib.h>
int atoi(const char *nptr);
  • 機能: 数値文字列を整数データに変換します。
  • パラメータ:
    • nptr: 指定された文字列
  • 戻り値:
    • 得られた造形データ
#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;
}

結果:
実行結果あとい

おすすめ

転載: blog.csdn.net/shuting7/article/details/130206272