943,994 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 2408
  • C RSS
0

Pattern Searching

by on Nov 2nd, 2005
Program to demonstrate pattern-searching of strings.
This program interactively builds a list of words and
searches for words in the list that match a specified pattern.
The pattern can include wildcard chars * and ?.
Eg : The query s* gives the words in the list which start with s.
The program uses a command-interpreter style interface for interaction with the user. Usage of the program is shown in the Usage() function.
The PatternSearch() function is the heart of the program. It returns 1 if a string pattern matches another in input.
The function has quite a few bugs I've not been able to put right. If
somebody can, please let me know.
C Code Snippet (Toggle Plain Text)
  1. /* pattern.c
  2.  * Program to demonstrate pattern searching.
  3.  * Written by : Vikram R. Bhat <vrb1001@yahoo.co.in>
  4.  *
  5.  * Program to demonstrate pattern-searching of strings.
  6.  * This program interactively builds a list of words and
  7.  * searches for words in the list that match a specified pattern.
  8.  * The pattern can include wildcard chars * and ?.
  9.  * Eg : The query s* gives the words in the list which start with s.
  10.  * The program uses a command-interpreter style interface for interaction
  11.  * with the user. Usage of the program is shown in the Usage() function.
  12.  * The PatternSearch() function is the heart of the program. It returns 1
  13.  * if a string pattern matches another in input.
  14.  * The function has quite a few bugs I've not been able to put right. If
  15.  * somebody can, please let me know.
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. #define INPUT 1
  22. #define QUERY 2
  23. #define MAXSIZE 20
  24. #define MAXCOL 30
  25.  
  26. void Usage( void );
  27. int PatternSearch( char *str1, char *str2 );
  28. char* StrUpper( const char *str );
  29.  
  30. int main()
  31. {
  32. char str[MAXSIZE][MAXCOL],input[MAXCOL];
  33. int mode = INPUT;
  34. int count = -1,i, m_count;
  35.  
  36. Usage();
  37.  
  38. while(1)
  39. {
  40. if( mode == INPUT )
  41. {
  42. printf("\n%d %s in list", count+1, count == 0 ? "item":"items" );
  43. printf("\n\nINPUT >");
  44. }
  45. else
  46. printf("\n\nQUERY > ");
  47.  
  48. fgets( input, MAXCOL, stdin );
  49. // fgets retains the newline entered. Replace it with null char.
  50. input[ strlen(input) - 1 ] = '\0';
  51.  
  52. if( !strcmp( StrUpper( input ), "$INPUT" ))
  53. {
  54. mode = INPUT;
  55. printf("\n[ Enter words to add to list ]");
  56. }
  57. else if( !strcmp( StrUpper( input ), "$QUERY" ))
  58. {
  59. mode = QUERY;
  60. printf("\n[ Enter pattern ( may contain wildcards * and ? ) to search in list. ]");
  61. }
  62. else if( !strcmp( StrUpper( input ), "$CLS" ))
  63. system("CLS");
  64. else if( !strcmp( StrUpper( input ), "$HELP" ))
  65. Usage();
  66. else if( !strcmp( StrUpper( input ), "$LIST" ))
  67. {
  68. for( i = 0; i<=count; i++ )
  69. printf("\n%s", str[i] );
  70. }
  71.  
  72. else if( !strcmp( StrUpper( input ), "$EXIT" ))
  73. break;
  74.  
  75. else
  76. {
  77. if( mode == INPUT )
  78. {
  79. if( count < MAXSIZE - 1 )
  80. strcpy( str[++count], input );
  81. else
  82. printf("\nSorry, cannot add.List Full");
  83. }
  84. else
  85. {
  86. m_count = 0;
  87. for( i=0; i<=count; i++ )
  88. {
  89. if( PatternSearch( str[i], input ))
  90. {
  91. m_count++;
  92. printf("\n%s", str[i] );
  93. }
  94. }
  95. printf("\n%d %s found.", m_count, m_count == 1 ? "match": "matches" );
  96. }
  97. }
  98. }
  99.  
  100. return 0;
  101. } // End of main
  102.  
  103. // How to use the program
  104. void Usage( void )
  105. {
  106. printf("Valid commands are ( case-insensitive ):");
  107. printf("\n\n $INPUT : Switch to input mode. Any input word\
  108. \n\texcept commands will be added to list");
  109. printf("\n\n $QUERY : Switch to query mode. The input word\
  110. \n\tis searched for pattern-match in the built list. Input\
  111. \n\tword can include the wildcards ? and *");
  112. printf("\n\n $LIST : List current words in list");
  113. printf("\n\n $CLS : Clear Screen");
  114. printf("\n\n $HELP : Display this message");
  115. printf("\n\n $EXIT : Exit this program\n");
  116. }
  117.  
  118. // Searches for pattern match of str2( can contain wildcards * and ? )
  119. // within str1
  120. int PatternSearch( char *str1, char *str2 )
  121. {
  122. int i,j, flag, asterisk_flag;
  123. i = j = asterisk_flag = 0;
  124. flag = 1;
  125.  
  126. while( str1[i] != '\0')
  127. {
  128.  
  129. if(str2[j] == '\0') // if str2 has ended, check
  130. { // for last char match
  131. if( str2[j-1] == '*' )
  132. break; // correct matching
  133. else if( str2[j-1] != str1[i-1] )
  134. {
  135. flag = 0;
  136. break;
  137. }
  138. }
  139. if( asterisk_flag ) // '*' found in str2, search for
  140. { // next char of str2 in str1
  141. flag = 0; // Assume no match
  142. if( str1[i] == str2[j] )
  143. {
  144. j++; // Move str2 char
  145. asterisk_flag = 0;
  146. flag = 1; // Match found
  147. }
  148. i++;
  149. }
  150.  
  151. else
  152. {
  153. if( str2[j] == '?' ) // Don't compare current chars
  154. {
  155. i++;
  156. j++;
  157. }
  158. else if( str2[j] == '*' ) // Search next char of str2
  159. { // in str1 ignoring anything
  160. j++; // in between
  161. asterisk_flag = 1;
  162. }
  163. else if( str2[j] == str1[i] )
  164. {
  165. i++;
  166. j++;
  167. }
  168. else // Matching failed
  169. return 0;
  170.  
  171. } // End of main 'else'
  172.  
  173. } // End of while
  174.  
  175. if( flag )
  176. return 1;
  177. else // str1 ended before a matching char following *
  178. return 0; // in str2 was found
  179. } // End of patternsearch
  180.  
  181. // Returns string in upper case
  182. char* StrUpper( const char *str )
  183. {
  184. int i;
  185. char *str2;
  186. str2 = ( char* ) malloc( sizeof( char)* strlen(str) + 1);
  187.  
  188. strcpy( str2, str );
  189.  
  190. for( i = 0; str[i] != '\0'; i++ )
  191. {
  192. if( islower( str2[i] ))
  193. str2[i]= toupper( str2[i] );
  194. }
  195. return str2;
  196. }
Message:
Previous Thread in C Forum Timeline: Removing Varibles from Memory
Next Thread in C Forum Timeline: New User





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC