① 用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]='