标签:string src puts clu ret length ++ cal inpu
1 //strlen - calculate the length of a string// 2 3 #include <stdio.h> 4 5 int strlens(const char *s); 6 char strcats(char *dest, const char *src); 7 8 #define NUM 100 9 10 int main(void) 11 { 12 char c[NUM]; 13 char *s; 14 s = c; 15 16 printf("Please input: "); 17 gets(s); 18 19 printf("The length of string is %d.\n",strlens(s)); 20 return 0; 21 } 22 23 24 int strlens(const char *s) 25 { 26 int i; 27 28 for (i = 0; *s++ != ‘\0‘; i++); 29 30 return i; 31 }
1 /* concatenate two strings */ 2 3 #include <stdio.h> 4 5 char* strcats(char *dest,const char *src); 6 7 int main(void) 8 { 9 char a[100],b[100]; 10 char *m,*n; 11 m = a; 12 n = b; 13 printf("Please input: "); 14 gets(m); 15 printf("Please input: "); 16 gets(n); 17 18 puts(m); 19 puts(n); 20 strcats(m,n); 21 22 puts(m) ; 23 24 return 0; 25 } 26 27 char* strcats(char *dest,const char *src) 28 { 29 int i = 0; 30 char *ch = dest; 31 32 while (*ch) { 33 ch++; 34 } 35 36 while (*ch++ = *src++); 37 38 return (dest); 39 }
1 #include <stdio.h> 2 3 char* strcpys(char * dest,const char * src); 4 5 int main(void) 6 { 7 char c[100],d[100], *s,*q; 8 s = c; 9 q = d; 10 printf("Please input: "); 11 gets(s); 12 printf("Please input: "); 13 gets(q); 14 15 strcpys(s,q); 16 puts(s); 17 // puts(q); 18 19 return 0; 20 } 21 22 char* strcpys(char * dest,const char * src) 23 { 24 while (*src != ‘\0‘) { 25 *dest++ = *src++; 26 } 27 *dest = ‘\0‘; 28 29 return dest; 30 31 }
1 #include <stdio.h> 2 3 strcmps(char *s, char *t); 4 5 int main(void) 6 { 7 int N; 8 char a[100],b[100],*s,*q; 9 s = a; 10 q = b; 11 12 printf("Please input: "); 13 gets(s); 14 printf("Please input: "); 15 gets(q); 16 17 N = strcmps(s,q); 18 printf("%d\n",N); 19 20 return 0; 21 } 22 23 strcmps(char *s, char *t) 24 { 25 while(*s==*t) 26 { 27 if(*s !=‘\0‘) 28 return 0; 29 ++s; ++t; 30 } 31 return (*s - *t); 32 }
标签:string src puts clu ret length ++ cal inpu
原文地址:https://www.cnblogs.com/frank-zhao19/p/10301888.html