Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int readline(char* text, int n);
- char* findmatch(char* pattern, char* text, int pl, int tl);
- void printmessage(char* position, char* text, int pl, int tl);
- int main() {
- char text[40], pattern[40], *position;
- int textlength, patternlength;
- printf("Enter text: ");
- textlength = readline(text, 40);
- printf("Enter pattern: ");
- patternlength = readline(pattern, 40);
- position = findmatch(pattern, text, patternlength, textlength);
- printmessage(position, text, patternlength, textlength);
- }
- int readline(char* text, int n) {
- int i=0;
- for(i;i<n;i++){
- int ic = getchar();
- if(ic == 10)
- break;
- else{
- *(text+i) = ic;
- }
- };
- return i;
- }
- char* findmatch(char* pattern, char* text, int pl, int tl) {
- int indexp = 0;
- char* position = NULL;
- int x=0;
- for(x; x<tl; x++){
- int pc = *(pattern+indexp);
- int tc = *(text+x);
- if(pc == 63 || pc == tc){
- indexp++;
- if(indexp == pl){
- position = (text+x)-indexp+1;
- break;
- }
- printf("status: passn");
- }else{
- indexp = 0;
- printf("status: failn");
- }
- }
- return position;
- }
- void printmessage(char* position, char* text, int pl, int tl) {
- if(position == NULL){
- printf("no matchn");
- }else{
- int p = (position-text)+1;
- printf("The pattern was found at char %d. The remaining text chars are: ", p);
- int i = p+pl-1;
- for(i; i<tl; i++){
- printf("%c", *(text+i));
- }
- printf("n");
- }
- }
- #include <stdio.h>
- #include <string.h>
- #define MAX_INPUT_LEN (40)
- // prototypes
- size_t myReadLine (char* text, int n);
- char* findmatch(char* pattern, char* text, size_t pl, size_t tl);
- void printmessage(char* position, char* text, size_t pl, size_t tl);
- int main( void )
- {
- char text[ MAX_INPUT_LEN ];
- char pattern[ MAX_INPUT_LEN ];
- char *position;
- size_t textlength;
- size_t patternlength;
- printf("Enter text: ");
- textlength = myReadLine( text, (int)(sizeof text) );
- printf("Enter pattern: ");
- patternlength = myReadLine( pattern, sizeof pattern );
- position = findmatch(pattern, text, patternlength, textlength);
- printmessage(position, text, patternlength, textlength);
- }
- size_t myReadLine( char* text, int n)
- {
- size_t i = 0; // initialize to no chars read
- if( NULL != fgets( text, n, stdin ) )
- {
- // eliminate trailing newline, if any
- char * newline;
- if( NULL != ( newline = strchr( text, 'n' ) ) )
- {
- *newline = '