拷贝字符串的代码,为什么输出不了。直接报错闪退
程序如下,新手求大佬教导#include <stdio.h>
char *FUN(char *x,char *y)
{
char *p=y;
for(;*x!='\0';x++,y++)
{
*y=*x;
}
*y='\0';
return p;
}
int main()
{
char *s,*q;char a[100];
gets(a);
s=a;
q=FUN(s,q);
puts(q);
return 0;
}
#include<stdio.h> #include<stdlib.h> #include<string.h> char* fun( char*,const char* ); int main( void ) { #define MAX 100 char s[MAX]; char p[MAX]; fgets(s,sizeof (s),stdin); s[strcspn(s,"\n")]='\0'; fun(p,s); puts(p); return 0; #undef MAX } char* fun( char* p,const char* q ) { const char* s=q; if (!p||!q) return ( char* )s; while (*p++=*q++); return ( char* )s; }
#include<stdio.h> #include<stdlib.h> #include<string.h> char* fun( char*,const char*,size_t size ); int main( void ) { #define MAX 100 char s[MAX]; char p[MAX]; fgets(s,sizeof (s),stdin); s[strcspn(s,"\n")]='\0'; fun(p,s,strlen(s)+1); puts(p); return 0; #undef MAX } char* fun( char* p,const char* q,size_t size ) { #ifndef DUFF_DECIVE #define DUFF_DECIVE(NUM,E,IS_END) \ do \ { \ size_t _Duff_Decive_Time=(~-NUM)>>3; \ \ if (NUM>0) \ switch (NUM&7) \ { \ do \ { \ case 0: E \ case 7: E \ case 6: E \ case 5: E \ case 4: E \ case 3: E \ case 2: E \ case 1: E \ }while \ ( \ _Duff_Decive_Time-->0##IS_END \ ); \ } \ }while (0) #endif const char* s=q; if (!p||!q) return ( char* )s; DUFF_DECIVE(size,if (!(*p++=*q++))break;,&&*(q-1)); return ( char* )s; #undef DUFF_DECIVE }
[此贴子已经被作者于2018-4-9 22:16编辑过]
/********************************************************************* * title: write a function "mystrncpy()",just as strncpy() do . * * C primer Plus charpter 11 exerices 11-13-07 *********************************************************************/ #include <stdio.h> #include<string.h> #define LEN 80 char *get_string(char *s2,int n); int get_int(void); char *mystrncpy(char *s1,char *s2,int n); int main (void) { char ch,s1[LEN],s2[LEN]; char *pt_s1,*pt_s2; int n; puts("do copy from s2 to s1 ?y/n?"); while(ch = getchar() == 'y') { while(getchar()!= '\n') continue; puts("Input a string,limited by 80 characters,or use '#' to end.:"); pt_s2 = get_string(s2,LEN); if(s2 == NULL) puts("receive failed."); else { puts("The string received is:"); //show the strings. puts(pt_s2); printf("Now,set how many characters to copy.\n"); n = get_int(); printf("copy %d characters of s2 to s1\n",n); pt_s1 = mystrncpy(s1,s2,n); printf("Now,s1[%d] = ",n); puts(pt_s1); while(getchar() != '\n') continue; } puts("Try again? y / n ? or input # to quit!"); } puts("DONE!"); return 0; } char *get_string(char *s2,int n) { int ch; int i; for(i = 0;i < n;i++) { ch = getchar(); if(ch != EOF && ch != '#') *(s2 + i) = ch; else if(ch == EOF) return NULL; else if(ch == '#') break; } *(s2 + i) = '\0'; return s2; } char *mystrncpy(char *s1,char *s2,int n) { int i,j; j = 0; for(i = 0;i < LEN;i++) { if(s2[i] != '\0') j++; else break; } if (j >= n) { for(i = 0;i < n;i++) s1[i] = s2[i]; s1[i] = '\0'; return s1; } else if(j < n) { for(i = 0;i < j;i++) s1[i] = s2[i]; s1[i] = '\0'; return s1; } } int get_int(void) { int n; while(scanf("%d",&n) != 1) { while(getchar() != '\n') continue; printf("input is not an integer type value,try again!"); } return n; }