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