日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置

發布時間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 字符串中查找字符 strchr()
    • 描述
    • 聲明
    • 參數
    • 返回值
  • 字符串分割 strtok()
    • 描述
    • 聲明
    • 參數
    • 返回值
  • 自己的函數

字符串中查找字符 strchr()

描述

C 庫函數 char *strchr(const char *str, int c) 在參數 str 所指向的字符串中搜索第一次出現字符 c(一個無符號字符)的位置。

聲明

下面是 strchr() 函數的聲明。

char *strchr(const char *str, int c)

參數

str – 要被檢索的 C 字符串。
c – 在 str 中要搜索的字符。

返回值

該函數返回在字符串 str 中第一次出現字符 c 的位置,如果未找到該字符則返回 NULL。

#include <stdio.h> #include <string.h>int main () {const char str[] = "http://www.runoob.com";const char ch = '.';char *ret;ret = strchr(str, ch);printf("|%c| 之后的字符串是 - |%s|\n", ch, ret);return(0); }

結果為:

|.| 之后的字符串是 - |.runoob.com|

字符串分割 strtok()

描述

C 庫函數 char *strtok(char *str, const char *delim) 分解字符串 str 為一組字符串,delim 為分隔符。

聲明

下面是 strtok() 函數的聲明。

char *strtok(char *str, const char *delim)

參數

str – 要被分解成一組小字符串的字符串。
delim – 包含分隔符的 C 字符串。

返回值

該函數返回被分解的第一個子字符串,如果沒有可檢索的字符串,則返回一個空指針。

#include <string.h> #include <stdio.h>int main () {char str[80] = "This is - www.runoob.com - website";const char s[2] = "-";char *token;/* 獲取第一個子字符串 */token = strtok(str, s);/* 繼續獲取其他的子字符串 */while( token != NULL ) {printf( "%s\n", token );token = strtok(NULL, s);}return(0); }

結果為:

This is www.runoob.com website

自己的函數

#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char str[] = "標簽坐標: X = 28889 cm , Y = 36 cm, Z = 8 cm";char *token;int XPos=0, YPos=0, ZPos=0;/* 獲取第一個子字符串 */token = strtok(str, " ");//printf("%s\n", token);/* 繼續獲取其他的子字符串 */while (token != NULL){ if (*token == 'X'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("XPos=%s\n", token);XPos = atoi(token);}if (*token == 'Y'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("YPos=%s\n", token);YPos = atoi(token);}if (*token == 'Z'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("ZPos=%s\n", token);ZPos = atoi(token);}token = strtok(NULL, " "); //printf("%s\n", token);}printf("%d\n", XPos);printf("%d\n", YPos);printf("%d\n", ZPos);return (0); }

結果為:

28889 36 8

Ref:

  • C 庫函數 - strchr()
  • C語言中常見的字符串處理函數
  • C 庫函數 - strtok()
  • 總結

    以上是生活随笔為你收集整理的【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。