本文目录一览:
- 1、C语言求子字符串个数
- 2、C语言求子串
- 3、C语言之求字符串的子串
- 4、自己写的C语言求子串函数求解答
- 5、C语言作业,求子串函数substr,我有程序,求大神给个设计思路
- 6、c语言中 编程实现求子串函数
C语言求子字符串个数
c:
#include stdio.h
#include stdlib.h
#include math.h
#include string.h
int countsub(char *str, char *ss) {
int len = strlen(str), index = 0, max = 0;
int *maxStr = (int*)malloc(sizeof(int) * len);
for (index = 0; index len; ++index) {
maxStr[index] = 0;
}
index = 0;
for (int i = 0; str[i] != '\0'; ++i) {
int j = 0;
for (; ss[j] != '\0' str[i + j] != '\0' ss[j] == str[i + j]; ++j);
if (j 0 ss[j] == '\0') {
maxStr[index]++;
i += j - 1;
} else {
index++;
}
if (maxStr[index] max)
max = maxStr[index];
}
return max;
}
int main() {
char s1[1000] = { 0 }, s2[100] = { 0 };
gets(s1);
gets(s2);
printf("%d\n", countsub(s1, s2));
return 0;
}
C语言求子串
#include stdio.h
#include stdlib.h
#include conio.h
#include string.h
#include malloc.h
#define MAXLEN 40
typedef struct
{
char ch[MAXLEN];
int len;
} SString;
int StrLength(SString s)
{
return(s.len);
}
int SubString(SString *sub,SString *s,int pos,int len)
{
int i;
if(pos0||poss-len||len1||lens-len-pos)
{
sub-len=0;
return(10);
}
else
{
for(i=0;ilen;i++)
{
sub-ch[i]=s-ch[i+pos];
}
sub-len=len;
return(1);
}
}
void main()
{
SString s, sub;
char buffer[128];
scanf("请输入一串字符串:%c",s.ch); /*1.请使用%S,2.请使用s.ch*/
strcpy(s.ch, "opqrst");/*为什么又把它覆盖掉*/
StrLength(SString);/*这句话什么意思,返回值为什么又被忽略,去掉就没有语法错误,逻辑错误多*/
printf("你输入的字符串长度为:%d\n",s.len);/*s.len未初始化*/
printf("主串为:opqrst\n",s.ch);/*这句话什么意思*/
s.len = 6;
SubString( sub, s, 3, 3);
memcpy(buffer, sub.ch, sub.len);
buffer[sub.len] = 10;
printf("子串为: %s\n",buffer);
}
===========================================
#include stdio.h
#include stdlib.h
#include conio.h
#include string.h
#include malloc.h
#define MAXLEN 40
typedef struct
{
char ch[MAXLEN];
int len;
} SString;
int SubString(SString *sub,SString *s,int pos,int len)
{
int i;
if(pos0||poss-len||len1||lens-len-pos)
{
sub-len=0;
return(0);
}
else
{
for(i=0;ilen;i++)
{
sub-ch[i]=s-ch[i+pos];
}
sub-len=len;
return(1);
}
}
void main()
{
SString s,sub;
char buffer[128];
clrscr();
scanf("%s",s.ch);
s.len=strlen(s.ch);
printf("你输入的字符串长度为;%d\n",s.len);
printf("主串为;%s\n",s.ch);
SubString(sub,s,3,3);/*后两个参数改成要求输入*/
/*memcpy(buffer,sub.ch,sub.len);*/
printf("子串为;%s\n",sub.ch);
getch();
}
SubString函数的逻辑错误就自己改吧
C语言之求字符串的子串
#include stdio.h
#include stdlib.h
#include string.h
char *sub(char *s,int st,int len)
{char *s1;
int i;
s1=(char*)malloc(len);
for(i=0;ilen;i++)s1[i]=s[st+i-1];
s1[i]='\0';
return s1;
}
int main()
{int n,i,j;
char s[200];
scanf("%d%*c",n);
while(n--)
{printf("input a string:");
gets(s);
printf("i=");
scanf("%d",i);
printf("j=");
scanf("%d%*c",j);
if(i+jstrlen(s))printf("Error\n");
else printf("%s\n",sub(s,i,j));
}
return 0;
}
自己写的C语言求子串函数求解答
#includestdio.h
#includestdlib.h
typedef struct hstring
{
char * ch;
int length;
} string;
void substring(string * str1,string str2,int i,int n)
{
if(n0 || nstr2.length-i+1 || i1 || istr2.length)
printf("fail!");
if(str1-ch) free(str1-ch);
if(!n) { /* 我的书里有这部分,你的没有? */
str1-ch = NULL;
str1-length = 0;
} else {
str1-ch=(char*)malloc(n*sizeof(char));
int j;
for(j=0; jn; ++j,++i) {
str1-ch[j]=str2.ch[i-1];
}
str1-length=str2.length;
}
}
int main()
{
string str1; /* str1 的空间是动态分配的 */
string str2 = { /* 目标串 str2 的空间是 已经分配好的 */
"mnpqrxyz",
8
};
/*
string str1,str2;
char s1[5]="abcd";
str1.ch=s1;
str1.length=4;
char s2[5]="mmmm";
str2.ch=s2;
str2.length=4;
*/
substring(str1,str2,1,3);
printf("%s",str1.ch);
return 0;
}
C语言作业,求子串函数substr,我有程序,求大神给个设计思路
char* substr(char *src,int start,int end){
if(end=start) return NULL;
if (start=strlen(src)) return NULL;
char *p=src+start;
*(p+end)=0;
return p;
}
c语言中 编程实现求子串函数
#include stdio.h
#include stdlib.h
char *substr(const char *s,int n1,int n2)/*从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址*/
{
char *sp=malloc(sizeof(char)*(n2-n1+2));
int i,j=0;
for (i=n1; i=n2; i++) {
sp[j++]=s[i];
}
sp[j]=0;
return sp;
}
int main(void)
{
char s[80],*sub;
scanf("%s",s);/*输入原字符串s*/
sub=substr(s,0,5);/*提取s[0]~s[5]元素组成新子串,并保存到sub中*/
printf("substr:%s\n",sub);/*输出sub*/
free(sub);/*释放sub所占用的空间*/
return 0;
}