#include #include "countchar.h" /* Generated by countchar.x */ /* CpSc 824 Desc: rpc server for countchar. This routine is passed a character string and a character via a pointer parameter to the enclosing structure -- and this routine counts and returns the number of occurrences of the character in the string. */ int *countchar_1(parm) stringdata *parm; /* Defined in countchar.x */ { static int count; char *p; /* Start at the begining of the string and scan through counting occurences */ count = 0; p = parm -> buf; while (*p) if (*p++ == parm-> findch) count++; /* Return count */ return(&count); } /* CpSc 824 Desc: rpc server countallchars. Scan the input parameter string and construct a linked list of each character found in the string and an occurence count. Return the linked list. */ nextcount *countallchars_1(bufptr) buftype *bufptr; { static nextcount start; /* Must be static since a pointer is return to it */ nextcount cur, last; char *ch; /* Free previous result - otherwise we will have a memory leak */ xdr_free(xdr_nextcount,&start); ch = *bufptr; /* Allocate first node */ start = malloc(sizeof(struct countdata)); start->character = *ch; start->count = 1; start->next = NULL; /* Now process rest of string */ ch++; while (*ch) { /* Search for previous occurence */ cur = start; while (cur) { if (cur->character == *ch) { cur->count++; break; } last = cur; cur = last->next; } /* If not found, allocate a new node */ if (cur == NULL) { last->next = cur = malloc(sizeof(struct countdata)); cur -> character = *ch; cur ->count=1; cur -> next = NULL; } ch++; } /* Return list */ return(&start); }