#include<stdio.h> #include<stdlib.h> char *substr(const char *s, int n1, int n2) { char *p = (char *) malloc(n2-n1+2); int i,j=0; for(i=n1;i<=n2;i++,j++) p[j] = s[i]; p[j] = '\0'; return p; } void main(void) { char s[80], *sub; int n1, n2; printf("請輸入原字符串:"); scanf("%s",s) ; printf("請輸入起止位置:"); scanf("%d%d",&n1,&n2); sub = substr(s,n1,n2); printf("子串爲:%s",sub); }
動態內存分配 學習筆記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.