Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int readline(char* text, int n);
  4. char* findmatch(char* pattern, char* text, int pl, int tl);
  5. void printmessage(char* position, char* text, int pl, int tl);
  6.  
  7. int main() {
  8. char text[40], pattern[40], *position;
  9. int textlength, patternlength;
  10.  
  11. printf("Enter text: ");
  12. textlength = readline(text, 40);
  13. printf("Enter pattern: ");
  14. patternlength = readline(pattern, 40);
  15. position = findmatch(pattern, text, patternlength, textlength);
  16. printmessage(position, text, patternlength, textlength);
  17. }
  18.  
  19. int readline(char* text, int n) {
  20. int i=0;
  21. for(i;i<n;i++){
  22. int ic = getchar();
  23. if(ic == 10)
  24. break;
  25. else{
  26. *(text+i) = ic;
  27. }
  28. };
  29. return i;
  30. }
  31.  
  32. char* findmatch(char* pattern, char* text, int pl, int tl) {
  33. int indexp = 0;
  34. char* position = NULL;
  35. int x=0;
  36. for(x; x<tl; x++){
  37. int pc = *(pattern+indexp);
  38. int tc = *(text+x);
  39. if(pc == 63 || pc == tc){
  40. indexp++;
  41. if(indexp == pl){
  42. position = (text+x)-indexp+1;
  43. break;
  44. }
  45. printf("status: passn");
  46. }else{
  47. indexp = 0;
  48. printf("status: failn");
  49. }
  50. }
  51. return position;
  52. }
  53.  
  54. void printmessage(char* position, char* text, int pl, int tl) {
  55. if(position == NULL){
  56. printf("no matchn");
  57. }else{
  58. int p = (position-text)+1;
  59. printf("The pattern was found at char %d. The remaining text chars are: ", p);
  60. int i = p+pl-1;
  61. for(i; i<tl; i++){
  62. printf("%c", *(text+i));
  63. }
  64. printf("n");
  65. }
  66. }
  67.  
  68. #include <stdio.h>
  69. #include <string.h>
  70.  
  71. #define MAX_INPUT_LEN (40)
  72.  
  73. // prototypes
  74. size_t myReadLine (char* text, int n);
  75. char* findmatch(char* pattern, char* text, size_t pl, size_t tl);
  76. void printmessage(char* position, char* text, size_t pl, size_t tl);
  77.  
  78. int main( void )
  79. {
  80. char text[ MAX_INPUT_LEN ];
  81. char pattern[ MAX_INPUT_LEN ];
  82. char *position;
  83. size_t textlength;
  84. size_t patternlength;
  85.  
  86. printf("Enter text: ");
  87. textlength = myReadLine( text, (int)(sizeof text) );
  88.  
  89. printf("Enter pattern: ");
  90. patternlength = myReadLine( pattern, sizeof pattern );
  91.  
  92. position = findmatch(pattern, text, patternlength, textlength);
  93. printmessage(position, text, patternlength, textlength);
  94. }
  95.  
  96.  
  97.  
  98. size_t myReadLine( char* text, int n)
  99. {
  100. size_t i = 0; // initialize to no chars read
  101.  
  102. if( NULL != fgets( text, n, stdin ) )
  103. {
  104. // eliminate trailing newline, if any
  105. char * newline;
  106. if( NULL != ( newline = strchr( text, 'n' ) ) )
  107. {
  108. *newline = '';
  109. }
  110.  
  111. i = strlen( text );
  112. }
  113. return i;
  114. }
  115.  
  116.  
  117.  
  118. char* findmatch(char* pattern, char* text, size_t pl, size_t tl)
  119. {
  120. size_t indexp = 0;
  121. char* position = NULL;
  122. size_t x=0;
  123.  
  124. for( ; x<tl; x++)
  125. {
  126. int pc = *(pattern+indexp);
  127. int tc = *(text+x);
  128.  
  129. if(pc == '?' || pc == tc)
  130. {
  131. indexp++;
  132. if(indexp == pl)
  133. {
  134. position = (text+x)-indexp+1;
  135. break;
  136. }
  137. printf("status: passn");
  138. }
  139.  
  140. else
  141. {
  142. indexp = 0;
  143. printf("status: failn");
  144. }
  145. }
  146. return position;
  147. }
  148.  
  149.  
  150. void printmessage(char* position, char* text, size_t pl, size_t tl)
  151. {
  152. if(position == NULL)
  153. {
  154. printf("no matchn");
  155. }
  156.  
  157. else
  158. {
  159. size_t p = (size_t)(position-text)+1;
  160. printf("%s: %ld. %sn",
  161. "The pattern was found at char",
  162. p,
  163. "The remaining text chars are: ");
  164. size_t i = p+pl-1;
  165.  
  166. for(; i<tl; i++)
  167. {
  168. printf("%c", *(text+i));
  169. }
  170. printf("n");
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement