123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <stdio.h>
- //#include "5.4.c"
- /* char *strncpy(s,ct,n) copy at most n characters of string ct to s; return s. Pad with '\0''s if ct has fewer than n characters. */
- /* char *strcat(s,ct) concatenate string ct to end of string s; return s. */
- //char *strcat(s,ct);
- /* int strncmp(cs,ct,n) compare at most n characters of string cs to string ct; return <0 if cs<ct, 0 if cs==ct, or >0 if cs>ct. */
- //int strncmp(cs,ct,n);
- char* strncpy(char* s,char* ct,int n);
- char *strcat(char* s,char* ct);
- int strncmp(char* cs,char* ct,int n);
- void teststrncpy();
- void teststrcat();
- void strncmptest();
- int main()
- {
- strncmptest();
- return(0);
- }
- void teststrcat()
- {
- char p1[100];
-
- for (int i=0;i<100;i++)
- p1[i]=0;
- for (int i=65;i<91;i++)
- p1[i-65]=(char)i;
-
- char *p2;
- p2="this is also a string";
- printf("%s",strcat(p1,p2));
- }
- void teststrncpy()
- {
- char *p1;
- p1="this is a string";
- char p2[25];
- char *ptop2 = p2;
- for (int i=0; i<25;i++)
- p2[i]=0;
- printf("%s",strncpy(p2,p1,5));
- }
- /* char *strncpy(s,ct,n) copy at most n characters of string ct to s; return s. Pad with '\0''s if ct has fewer than n characters. */
- char* strncpy(char* s,char* ct,int n)
- {
- char* tempptr=s;
- if (ct == 0) return ct;
- for (int i =0; i<n; i++) // limit to max n chars
- {
- if ((*ct)!=0)
- {
- (*tempptr)= (*ct);
- tempptr++;
- ct++;
- }
- else
- {
- (*tempptr)=0;
- return s;
- }
- }
- return s;
- }
- /* char *strcat(s,ct) concatenate string ct to end of string s; return s. */
- /* no checking AT ALL on this one. good luck with yer bounds*/
- char *strcat(char* s,char* ct)
- {
- char* tempptr= s; //we'll need this for returning to the calling function
- if (ct==0) return 0; //fail if there's no string to copy.
- while (*s!=0) s++; //find spot in output string to start copying to.
- s--; //off by one
- do
- {
- s++;
- *s=*ct;
- ct++;
- }
- while ( *ct!=0);
- s++;
- *s=0; //append trailing 0
- return tempptr; //return string
- }
- void strncmptest()
- {
- printf("%d \n",strncmp("12345","12345",3));
- }
- /*compare at most n characters of string cs to string ct; return <0 if cs<ct, 0 if cs==ct, or >0 if cs>ct. */
- int strncmp(char* cs,char* ct,int n)
- {
- if ((ct==0) && (ct==0)) return -6;
- if (cs==0) return -2;
- if (ct==0) return -4;
-
- for (int c=0; c<=n;c++)
- {
- if ((*cs)>(*ct)) return 1;
- else if ((*cs)<(*ct)) return -1;
- else
- {
- ct++;
- cs++;
- }
- }
-
- return 0;
- }
-
-
-
- /* int strncmp(char cs[],char ct[],int n)
- {
- if (cs == 0) return -2;
- if (ct == 0) return -2;
- for (int i=0; i < n; i++) //limit n;
- {
- if (*cs == *ct)
- i++;
- if (*cs > *ct) return
- return 1;
- if (*cs < *ct) return
- return -1;
- }
- return 0;
- }
- */
|