模擬實現strchr函式
程式碼:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> char*Strchr(const char*str, int ch) { assert(str); char*pstr = (char*)str; while (*pstr!='\0') { if (*pstr==ch) { return pstr; } pstr++; } return NULL; } int main() { char*p = "abcdcfg"; char*q=Strchr(p, 'c'); if (q != NULL) { printf("找到了\n"); printf("%s", q); } else { printf("沒找到\n"); } system("pause"); return 0; }
相關推薦
模擬實現strchr函式
程式碼: #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<
【C語言】模擬實現strchr函式.即在一個字串中查詢一個字元第一次出現的位置並返回
//模擬實現strchr函式.即在一個字串中查詢一個字元第一次出現的位置並返回 #include <stdio.h> //#include <string.h> #includ
【c語言】模擬實現strchr函式,功能:在一個字串中查詢一個字元第一次出現的位置,如果沒有出現返回NULL
// 模擬實現strchr函式,功能:在一個字串中查詢一個字元第一次出現的位置,如果沒有出現返回NULL #include <stdio.h> #include <assert.h> char const* my_strchr(char cons
編寫函式,模擬實現strchr
編寫函式,模擬實現strchr 程式程式碼如下: #include <stdio.h> #include <assert.h> //找出某個字元在字串中第一次出現的地址 char *MyStrchr(const char *dest,
C語言:模擬實現字串函式strlen,strcpy,strcat,strcmp,strchr,strstr
什麼是C語言中的字串 字串或串(String)是由數字、字母、下劃線組成的一串字元。通常放在常量字串中或者字元陣列中。C語言中以 ‘\0’ 來作為字串的結束標記。 字元的ASCII編碼表 1.strlen 功能:字串求長 計算給定字串的(unsigned in
模擬實現庫函式strchr、strrchr、memchr
①strchr函式【函式原型】:char* strchr(constchar* string,int c);【函式功能】:在一個字串中查詢指定字元的第一次出現的位置。如果查詢成功,返回該字元的地址,如果
模擬實現strcpy函式功能(優化改進)
strcpy函式,字串拷貝函式,傳入兩個引數,將第二個引數的值拷貝到第一個中去。 首先,給出一個普通的程式碼: #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> void
自己實現strchr函式與strstr函式
char * Strchr(char to[], char Ch) { int i = 0; while (to[i] != Ch && to[i] != '\0') i++; return to[i] != '\0' ? to + i :
模擬實現strlen函式
目錄 方式1(遞迴) 方式2(迴圈) 方式3(指標運算) 正文 方式1(遞迴) int my_strlen1(char *str) { if (*str == '\0') {
模擬實現strcmp函式
strcmp int strcmp ( const char * str1, const char * str2 ); Compare two strings Compares the C string str1 to the C string str2. This f
C語言模擬實現庫函式strlen的三種方法
學習之路無止境,漫漫長夜,我愛程式設計。 今天練習的是用C語言來簡單實現庫函式strlen。 首先strlen 函式的作用是求出字串長度,眾所周知,在C語言中,字串是以字串陣列的形式儲存的,並且以’\0’為結束標誌。先讓我們看看strlen庫函式的引數即返回值
模擬實現strchr
今天 模擬實現的是strchr這個庫函式,還是首先上來簡單介紹一下這個函式是用來做什麼的。 先看看c++官網的正規介紹 看不懂的話可以看一下百度給的簡單通俗易懂的解釋 其實就是strstr是在
模擬實現strchr和strrchr
模擬實現strchr() 原型:char *strchr( const char *string, char ch ); 所在庫名:#include <string.h> 功能:找出在字串
模擬實現strchr.strrchr
模擬實現strchr strchr函式返回要查詢字元子字串中第一次出現的地址 #include <stdio.h> #include <stdlib.h> #include <assert.h> char *my_strc
面試題 模擬實現strlen函式的3種方法。
第一種方法是用計數器模擬實現strlen函式。#include<stdio.h> #include<assert.h> int my_strlen(const char *str) { int count = 0; assert(str!=NUL
string庫之模擬實現strchr、strrchr與memchr
原型: C庫string.h中的strchr函式 描述: C 庫函式 char *strchr(const char *str, int c) 在引數 str 所指向的字串中搜索第一次出現字元 c(一個無符號字元)的位置。 引數: str:要被檢索的
模擬實現 strchr
strchr函式 函式功能:查詢一個字元。在一個字串中查詢一個特定的字元。 函式原型:char *strchr(char const *str,int ch); 函式說明:strchr在字串str中查詢字元ch第一次出現的位置,找到後返回一個指向該位置的指
【C語言】自己編寫程式實現strchr函式。即在給定字串中找特定的字元並返回該處指標。
<pre name="code" class="cpp">//自己編寫程式實現strchr函式。即在給定字串中找特定的字元並返回該處指標。 #include <stdio.h>
用可變引數列表模擬實現printf函式
用可變引數列表實現printf函式要實現printf函式,我們首先應該獲取printf函式中由雙引號中的字串,若遇到不是%的字元,我們應該直接將其輸出,若遇到%,我們應該判斷它後面是什麼字元,這裡先只考慮%c,%s的情況,如果是%c那麼我們應該將讀到的字元輸出來,若為%s,應
模擬實現strstr函式
strstr 函式簡介 函式原型: char *strstr( const char *string, const char *strCharSet ); 函式功能:搜尋一個字串在另一個