5.5.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdio.h>
  2. //#include "5.4.c"
  3. /* 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. */
  4. /* char *strcat(s,ct) concatenate string ct to end of string s; return s. */
  5. //char *strcat(s,ct);
  6. /* 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. */
  7. //int strncmp(cs,ct,n);
  8. char* strncpy(char* s,char* ct,int n);
  9. char *strcat(char* s,char* ct);
  10. int strncmp(char* cs,char* ct,int n);
  11. void teststrncpy();
  12. void teststrcat();
  13. void strncmptest();
  14. int main()
  15. {
  16. strncmptest();
  17. return(0);
  18. }
  19. void teststrcat()
  20. {
  21. char p1[100];
  22. for (int i=0;i<100;i++)
  23. p1[i]=0;
  24. for (int i=65;i<91;i++)
  25. p1[i-65]=(char)i;
  26. char *p2;
  27. p2="this is also a string";
  28. printf("%s",strcat(p1,p2));
  29. }
  30. void teststrncpy()
  31. {
  32. char *p1;
  33. p1="this is a string";
  34. char p2[25];
  35. char *ptop2 = p2;
  36. for (int i=0; i<25;i++)
  37. p2[i]=0;
  38. printf("%s",strncpy(p2,p1,5));
  39. }
  40. /* 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. */
  41. char* strncpy(char* s,char* ct,int n)
  42. {
  43. char* tempptr=s;
  44. if (ct == 0) return ct;
  45. for (int i =0; i<n; i++) // limit to max n chars
  46. {
  47. if ((*ct)!=0)
  48. {
  49. (*tempptr)= (*ct);
  50. tempptr++;
  51. ct++;
  52. }
  53. else
  54. {
  55. (*tempptr)=0;
  56. return s;
  57. }
  58. }
  59. return s;
  60. }
  61. /* char *strcat(s,ct) concatenate string ct to end of string s; return s. */
  62. /* no checking AT ALL on this one. good luck with yer bounds*/
  63. char *strcat(char* s,char* ct)
  64. {
  65. char* tempptr= s; //we'll need this for returning to the calling function
  66. if (ct==0) return 0; //fail if there's no string to copy.
  67. while (*s!=0) s++; //find spot in output string to start copying to.
  68. s--; //off by one
  69. do
  70. {
  71. s++;
  72. *s=*ct;
  73. ct++;
  74. }
  75. while ( *ct!=0);
  76. s++;
  77. *s=0; //append trailing 0
  78. return tempptr; //return string
  79. }
  80. void strncmptest()
  81. {
  82. printf("%d \n",strncmp("12345","12345",3));
  83. }
  84. /*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. */
  85. int strncmp(char* cs,char* ct,int n)
  86. {
  87. if ((ct==0) && (ct==0)) return -6;
  88. if (cs==0) return -2;
  89. if (ct==0) return -4;
  90. for (int c=0; c<=n;c++)
  91. {
  92. if ((*cs)>(*ct)) return 1;
  93. else if ((*cs)<(*ct)) return -1;
  94. else
  95. {
  96. ct++;
  97. cs++;
  98. }
  99. }
  100. return 0;
  101. }
  102. /* int strncmp(char cs[],char ct[],int n)
  103. {
  104. if (cs == 0) return -2;
  105. if (ct == 0) return -2;
  106. for (int i=0; i < n; i++) //limit n;
  107. {
  108. if (*cs == *ct)
  109. i++;
  110. if (*cs > *ct) return
  111. return 1;
  112. if (*cs < *ct) return
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. */