本文目錄一覽:
- 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;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/152941.html