Implementation of string functions such as replace, indexof, and substring in Linux C language)

Source: Internet
Author: User

C language is not as rich as a string operation function as Java. Many useful functions have to be written by themselves. After a day, several common functions have been written, which will be useful in the future.

 

[CPP]View plaincopy

  1. # Include <string. h>
  2. # Include <stdio. h>
  3. /* Replace the str2 string that appears for the first time in the str1 string with str3 */
  4. Void replacefirst (char * str1, char * str2, char * str3)
  5. {
  6. Char str4 [strlen (str1) + 1];
  7. Char * P;
  8. Strcpy (str4, str1 );
  9. If (P = strstr (str1, str2 ))! = NULL)/* P points to the first position of str2 in str1 */
  10. {
  11. While (str1! = P & str1! = NULL)/* move the str1 pointer to the position of P */
  12. {
  13. Str1 ++;
  14. }
  15. Str1 [0] = '\ 0';/* converts the value pointed by str1 pointer to/0 to intercept str1 and discard str2 and subsequent content, only content earlier than str2 is retained */
  16. Strcat (str1, str3);/* concatenate str3 after str1 to form a new str1 */
  17. Strcat (str1, strstr (str4, str2) + strlen (str2);/* strstr (str4, str2) points to str2 and later content (including str2 ), strstr (str4, str2) + strlen (str2) is to move the Pointer Forward to the strlen (str2) bit, skip str2 */
  18. }
  19. }
  20. /* Replace all str2 in str1 with str3 */
  21. Void Replace (char * str1, char * str2, char * str3)
  22. {
  23. While (strstr (str1, str2 )! = NULL)
  24. {
  25. Replacefirst (str1, str2, str3 );
  26. }
  27. }
  28. /* In the SRC string, the string starting from subscript start to end-1 (before end) is saved in DEST (subscript starts from 0 )*/
  29. Void substring (char * DEST, char * SRC, int start, int end)
  30. {
  31. Int I = start;
  32. If (Start> strlen (SRC) return;
  33. If (end> strlen (SRC ))
  34. End = strlen (SRC );
  35. While (I <End)
  36. {
  37. Dest [I-start] = SRC [I];
  38. I ++;
  39. }
  40. Dest [I-start] = '\ 0 ';
  41. Return;
  42. }
  43. /* Returns the index character in SRC */
  44. Char charat (char * SRC, int index)
  45. {
  46. Char * P = SRC;
  47. Int I = 0;
  48. If (index <0 | index> strlen (SRC ))
  49. Return 0;
  50. While (I <index) I ++;
  51. Return P [I];
  52. }
  53. /* Returns the position of str2 that appears in str1 for the first time (index in the following table).-1 */
  54. Int indexof (char * str1, char * str2)
  55. {
  56. Char * P = str1;
  57. Int I = 0;
  58. P = strstr (str1, str2 );
  59. If (P = NULL)
  60. Return-1;
  61. Else {
  62. While (str1! = P)
  63. {
  64. Str1 ++;
  65. I ++;
  66. }
  67. }
  68. Return I;
  69. }
  70. /* Returns the position (subscript) of the last occurrence of str2 in str1.-1 */
  71. Int lastindexof (char * str1, char * str2)
  72. {
  73. Char * P = str1;
  74. Int I = 0, Len = strlen (str2 );
  75. P = strstr (str1, str2 );
  76. If (P = NULL) Return-1;
  77. While (P! = NULL)
  78. {
  79. For (; str1! = P; str1 ++) I ++;
  80. P = P + Len;
  81. P = strstr (p, str2 );
  82. }
  83. Return I;
  84. }
  85. /* Delete the white space (space character and horizontal tab) before the first non-white space character on the left of Str )*/
  86. Void ltrim (char * Str)
  87. {
  88. Int I = 0, J, Len = strlen (STR );
  89. While (STR [I]! = '\ 0 ')
  90. {
  91. If (STR [I]! = 32 & STR [I]! = 9) break;/* 32: space, 9: horizontal tab */
  92. I ++;
  93. }
  94. If (I! = 0)
  95. For (j = 0; j <= len-I; j ++)
  96. {
  97. STR [J] = STR [J + I];/* move the subsequent characters forward and add the deleted blank position */
  98. }
  99. }
  100. /* Delete all blank characters (space characters and horizontal tabs) after the last non-blank character of Str )*/
  101. Void rtrim (char * Str)
  102. {
  103. Char * P = STR;
  104. Int I = strlen (STR)-1;
  105. While (I> = 0)
  106. {
  107. If (P [I]! = 32 & P [I]! = 9) break;
  108. I --;
  109. }
  110. STR [++ I] = '\ 0 ';
  111. }
  112. /* Delete the blank characters at both ends of Str */
  113. Void trim (char * Str)
  114. {
  115. Ltrim (STR );
  116. Rtrim (STR );
  117. }

 

 

Save as mystr. C, and create the header file mystr. h:

 

[CPP]View plaincopy

  1. Extern void replacefirst (char * str1, char * str2, char * str3 );
  2. Extern void Replace (char * str1, char * str2, char * str3 );
  3. Extern void substring (char * DEST, char * SRC, int start, int end );
  4. Extern char charat (char * SRC, int index );
  5. Extern int indexof (char * str1, char * str2 );
  6. Extern int lastindexof (char * str1, char * str2 );
  7. Extern void ltrim (char * Str );
  8. Extern void rtrim (char * Str );
  9. Extern void trim (char * Str );

 

 

Write another test file test. C:

 

[CPP]View plaincopy

  1. # Include <string. h>
  2. # Include <stdio. h>
  3. # Include "mystr. H"
  4. Void main ()
  5. {
  6. Char Buf [20] = "012345126 ";
  7. Char buf2 [10];
  8. Replacefirst (BUF, "12", "9999 ");
  9. Printf ("replacefirst: % s/n", Buf );
  10. Strcpy (BUF, "012345126 ");
  11. Replace (BUF, "12", "9999 ");
  12. Printf ("Replace: % s/n", Buf );
  13. Strcpy (BUF, "01234560 ");
  14. Substring (buf2, Buf, 2, 5 );
  15. Printf ("substring: % s/n", buf2 );
  16. Printf ("charat: % C/N", charat (BUF, 4 ));
  17. Printf ("indexof: % d/N", indexof (BUF, "234 "));
  18. Printf ("lastindexof: % d/N", lastindexof (BUF, "0 "));
  19. Strcpy (BUF, "0123 ");
  20. Ltrim (BUF );
  21. Printf ("ltrim: | % S |/N", Buf );
  22. Strcpy (BUF, "0123 ");
  23. Rtrim (BUF );
  24. Printf ("rtrim: | % S |/N", Buf );
  25. Strcpy (BUF, "0123 ");
  26. Trim (BUF );
  27. Printf ("Trim: | % S |/N", Buf );
  28. Strcpy (BUF ,"");
  29. Trim (BUF );
  30. Printf ("trim2: | % S |/N", Buf );
  31. }

 

 

Input in Shell

GCC test. c mystr. C-o Str

./Str

Run.

The result is as follows:

 

Replacefirst: 09999345126

Replace: 0999934599996

Substring: 234

Charat: 4

Indexof: 2

Lastindexof: 7

Ltrim: | 0123 |

Rtrim: | 0123 |

Trim: | 0123 |

Trim2: |

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.