#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); }
原文地址:http://9815936.blog.51cto.com/9805936/1638331