A

#### arnuld

rightmost occurence of t in s, or -1 if there is none.

PURPOSE: this program prints the index of the rightmost match on the line.

The match we have to find is a char array named pattern. We also print out

the number of matches we have found. We will take the input from

command-line.

PROBLEM: Segmentation Fault

The programe compiles fine but at run-time it segfaults

here is my solution which is a little modified version of the example

provided in the same section:

/* Exercise # 4.1 */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

enum MAXSIZE { ARR_SIZE = 1000 };

int getline( char current_line[], int max );

int str_index( char current_line[], char search_for[] );

char pattern[] = "term";

int match_num;

int main( void )

{

char current_line[ARR_SIZE];

int matched_idx, idx;

idx = 0;

while( getline(current_line, ARR_SIZE) > 0 )

{

if( (matched_idx = str_index(current_line, pattern)) >= 0 )

{

printf("\n%d matches, \n%d is the last to match", match_num, matched_idx);

}

}

return 0;

}

/* takes a line as input and returns the length of the line */

int getline( char s[], int max )

{

int c, i;

for( i = 0; ( (c = getchar()) != EOF && (c != '\n') && (--max > 0) ); ++i )

{

s

*= c;*

}

if( c == '\n' )

{

s[i++] = '\n';

}

s

}

if( c == '\n' )

{

s[i++] = '\n';

}

s

*= '\0';*

return i;

}

/* search for a pattern in the line, will save every index position of

source string where the pattern starts to match. For string the index we

use an array of ints. we then return the last element of array which is the

index of the rightmost match. We also return the number of matches we have

found using an int match_num.

*/

int str_index( char s[], char p[] )

{

int i, j, k;

int idx, last_match;

int saved_pos[ARR_SIZE];

memset( saved_pos, '\0', sizeof( saved_pos ));

idx = 0;

match_num = 0;

for( i = 0; sreturn i;

}

/* search for a pattern in the line, will save every index position of

source string where the pattern starts to match. For string the index we

use an array of ints. we then return the last element of array which is the

index of the rightmost match. We also return the number of matches we have

found using an int match_num.

*/

int str_index( char s[], char p[] )

{

int i, j, k;

int idx, last_match;

int saved_pos[ARR_SIZE];

memset( saved_pos, '\0', sizeof( saved_pos ));

idx = 0;

match_num = 0;

for( i = 0; s

*!= '\0'; ++i )*

{

for( k = 0, j = i; p[k] != '\0' ; ++k, ++j )

{

if( s[j] != p[k] )

{

break;

}

}

if( (k > 0) && p[k] == '\0' )

{

saved_pos[idx] = i;

++match_num;

}

}

last_match = sizeof(saved_pos) - 2;

/* it checks whether we have any match or not. If we do not have any match

* then we have nothing in array */

if( saved_pos[0] )

{

return saved_pos[last_match];

}

else

{

return -1;

}

}

=========== OUTPUT =============

/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 4-1.c

/home/arnuld/programs/C $ ./a.out

terminal

and no one

and this

term

and Xterminal

segmentation fault

/home/arnuld/programs/C ${

for( k = 0, j = i; p[k] != '\0' ; ++k, ++j )

{

if( s[j] != p[k] )

{

break;

}

}

if( (k > 0) && p[k] == '\0' )

{

saved_pos[idx] = i;

++match_num;

}

}

last_match = sizeof(saved_pos) - 2;

/* it checks whether we have any match or not. If we do not have any match

* then we have nothing in array */

if( saved_pos[0] )

{

return saved_pos[last_match];

}

else

{

return -1;

}

}

=========== OUTPUT =============

/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 4-1.c

/home/arnuld/programs/C $ ./a.out

terminal

and no one

and this

term

and Xterminal

segmentation fault

/home/arnuld/programs/C $