導航:首頁 > 源碼編譯 > c語言substr函數源碼

c語言substr函數源碼

發布時間:2022-07-23 22:13:20

① 用c語言編寫一個函數,void substr(chars[],int start,int l

void substr(char s[], int start, int len)
{
char * r = s;
if( start < 0 || len < 0)

return;

int ct = 0;
while( *s != '\0' && ct < start )
s++,ct++;

ct = 0;

while( *s != '\0' && ct < len)
*r++ = *s++,ct++;
*r='\0';

}

② 麻煩講解一下C語言中substr函數的用法

c語言標准庫裡面沒這個函數,如果你在代碼中看到了這個函數,那一定是自定義的,沒辦法講解用法。
但是c++裡面有這個方法(從根本上來說應該叫方法,不是函數),我給你講講c++裡面這個函數的用法吧:
這個函數的原型是:basic_string substr( size_type index, size_type num = npos );

substr()返回本字元串的一個子串,從index開始,長num個字元。如果沒有指定,將是默認值
string::npos。這樣,substr()函數將簡單的返回從index開始的剩餘的字元串。

例如:
string s("What we have here is a failure to communicate");

string sub = s.substr(21);

cout << "The original string is " << s << endl;
cout << "The substring is " << sub << endl;

顯示:
The original string is What we have here is a failure to communicate
The substring is a failure to communicate

③ 請問C++中的substr()函數要怎麼用

1、SUBSTR()函數是VFP字元函數的一種。表示的是字元型函數。
2、它的格式是:SUBSTR(<字元表達式>、<數值表達式1>[,<數值表達式2>]
3、功能:是從給定的字元表達式或備注欄位中返回一個子字元串。
4、範例:
STORE'abcdefghijklm'
To
mystring
?SUBSTR(mystring
,1,5)
顯示
"abcde"
?SUBSTR(mystring
,6)
顯示
"fghijklm"
?SUBSTR(mystring,-2)顯示「lm」
?SUBSTR(mystrng,-4)顯示「jklm」、
5、格式二:SUBSTR(char
A,char
B,int
C,
int
D)
(1)這個函數主要用於字元串的運算,參數A是字元數組或是指向字元串的指針,用於接收字元串,參數B一般也為字元數組或指向字元串的指針,表示要截取字元串的地方,參數C表示從B中截取字元串的起始位置,參數D表示要截取字元串的長度,預設時返回字元表達式的值結束前B的全部字元。
上述表達式功能可描述為:從字元串B的第C個字元處開始,截取長度為D的一串字元串,放入字元串數組A中,返回截取的字元串。
(2)範例:ch
X
S[]="abcdefgh"
調用substr(X,S,4,2)後,得到的結果是:"ef"。

④ C語言作業,求子串函數substr,我有程序,求大神給個設計思路

⑤ 求C語言大神幫忙處理一下程序啊 編寫求子串函數substr(s,n1,n2),在串s中從n1位置開始取n2個字元的子串。

charstr[128];

intn1,n2;

(1)輸入主串;scanf("%s",str);

(2)輸出主串;printf("%s ",str);

(3)輸入開始位置與子串長度;scanf("%d%d",&n1,&n2);

(4)輸出取出的子串;printf("%s ",substr(str,n1,n2);

(5)退出系統;exit(0);

char*substr(char*s,intn1,intn2)
{
staticstr[128];
inti=0;
for(i=0;i<n2;i++)
str[i]=*(s+n1+i);
str[i]='';
returnstr;
}

⑥ c語言substr函數是什麼意思

c語言標准庫函數中是沒有substr函數的,除非你自定義實現。

c++語言標准庫中的string類包含了一個substr函數。


在MSDN中,關於該函數的描述如下:

函數原型:

basic_string substr(size_type pos = 0, size_type n = npos) const;


功能描述:

The member function returns an object whose controlled sequence is a of
up to n elements of the controlled sequence beginning at position pos.

該函數返回一個包含了當前字元串從pos位置開始到第n個字元的子串對象副本。


函數參數:

pos字元串截取的開始位置,從0開始計數。

n截取的字元長度,如果大於當前字元串可截取的有效字元長度,則默認截取有效長度


舉例如下:

#include<stdlib.h>
#include<string>
usingnamespacestd;

intmain()
{
stringsTest="Thisisatest!";
stringsSub=sTest.substr(0,4);

printf("%s %d",sSub.c_str());
return0;
}

⑦ 求C語言中strlen()函數的源代碼

unsigned int strlen(const char *s)
{
int len = 0;
while(*s++)
len++;
return len;
}

⑧ (C語言)幫忙編寫求子串函數substr(s,n1,n2)

void substr(char s[],int n1,int n2)
{int i;<br/>char t[20]={\0};
for(i=n1;i<=n1+n2;i++)
t=strcat(t,s[i]);
puts(t);
}
這個函數要用到
#include<stdio.h>
#include<string.h>

⑨ c語言中有沒有substr函數

沒有這個函數。

strstr()函數用來檢索子串在字元串中首次出現的位置,其原型為:
char *strstr( char *str, char * substr );
【參數說明】str為要檢索的字元串,substr為要檢索的子串。
【返回值】返回字元串str中第一次出現子串substr的地址;如果沒有檢索到子串,則返回NULL。
頭文件:#include <string.h>
【函數示例】strstr()函數的使用。
#include<stdio.h>
#include<string.h>
int main(){
char *str = "http://see.xidian.e.cn/cpp/u/xitong/";
char *substr = "see";
char *s = strstr(str, substr);
printf("%s\n", s);
return 0;
}
運行結果:
see.xidian.e.cn/cpp/u/xitong/

閱讀全文

與c語言substr函數源碼相關的資料

熱點內容
武漢理工大學伺服器ip地址 瀏覽:139
亞馬遜雲伺服器登錄 瀏覽:515
安卓手機如何進行文件處理 瀏覽:62
mysql執行系統命令 瀏覽:920
php支持curlhttps 瀏覽:134
新預演算法責任 瀏覽:435
伺服器如何處理5萬人同時在線 瀏覽:242
哈夫曼編碼數據壓縮 瀏覽:415
鎖定伺服器是什麼意思 瀏覽:376
場景檢測演算法 瀏覽:608
解壓手機軟體觸屏 瀏覽:339
方舟pv怎麼轉伺服器 瀏覽:100
數據挖掘中誤差值演算法函數 瀏覽:119
php開發套件 瀏覽:191
伺服器的spi板是什麼 瀏覽:897
解壓縮全能王中文密碼是什麼 瀏覽:80
javaftp伺服器上傳文件 瀏覽:104
演算法設計中文版pdf 瀏覽:82
視頻壓縮形式怎麼改 瀏覽:369
perl程序員 瀏覽:791