求子串c語言,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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-13 06:08
下一篇 2024-11-13 06:08

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28
  • Python作為中心語言,在編程中取代C語言的優勢和挑戰

    Python一直以其簡單易懂的語法和高效的編碼環境而著名。然而,它最近的發展趨勢表明Python的使用範圍已經從腳本語言擴展到了從Web應用到機器學習等廣泛的開發領域。與此同時,C…

    編程 2025-04-28
  • Python基礎語言

    Python作為一種高級編程語言擁有簡潔優雅的語法。在本文中,我們將從多個方面探究Python基礎語言的特點以及使用技巧。 一、數據類型 Python基礎數據類型包括整數、浮點數、…

    編程 2025-04-28

發表回復

登錄後才能評論