str_cpy(char *p,char b[],int m)
{char *q,q=b;
while(m>0)
{p++;
m--;
}
while(*q!="\0")
{*q=*p;
p++;
q--;
}
*q="\0"
}
#include <stdio.h> #include <stdlib.h> #include <conio.h>
#define POS 3
long str_len(char *); void str_cpy(char*,char[],int);
int main() { char *str1="Hello"; char *str2; str2=(char*)malloc((str_len(str1)-POS)*sizeof(char)+1); if(!str2) {puts("Cannot allocate memory."); return 0;} str_cpy(str1,str2,POS); puts(str2); free(str2); getch(); return 1; }
long str_len(char *s) { long result=0; for(;*s++;result++); return result; }
void str_cpy(char *p,char b[],int m) { p+=m; while(*p) {*b=*p++; b++;} *b='\0'; }
[此贴子已经被作者于2005-4-24 10:49:47编辑过]
#include <stdio.h> #include <stdlib.h> #include <conio.h>
int main() { char *str1; char *str2; str_cpy(char *p,char b[],int m) puts(str2); free(str2); getch(); return 1; } str_cpy(char *p,char b[],int m) {char *q,q=b; while(m>0) {p++; m--; } while(*q!="\0") {*q=*p; p++; q--; } *q="\0" } 这样写可以吗 :) 帮我看看啊
//编一程序,把字符串中的第m个字符开始的全部字符复制成另一个字符串, //求在主函数中输入字符串及m的值并输出复制结果,在被调函数中完成复制. #include <stdio.h> #include <string.h>
#define MAXCHARACTER 100 static char str1[MAXCHARACTER]; static char str2[MAXCHARACTER];
char * Str_copy(int );
char * Str_copy(int m) { int str_1_length; str_1_length=strlen(str1); for(int i=m-1;i<str_1_length;i++) str2[i-m+1]=str1[i]; str2[i-m+1]='\0'; return str2; }
void main() { int length; int m; printf("please enter the str1:\n"); gets(str1); length=strlen(str1); do{ printf("please enter the position of beginning copying(1~%d):\n",length); scanf("%d",&m); }while(m<1||m>length); puts(Str_copy(m)); }