/*Program for seeking a substring in a given string and replacing it's occorance with another given string*/ /*Program strenths: replaces substring with same, smaller or bigger size string, pre-determines overflow*/ /*Program weaknesses: replaces only first occorance: but modular enough to be modified to replace all of the occorances or any targetted one*/ #include #include /*define a size limit for the string lengths to be used*/ #define SIZ 100 void chksiz(int ); void remnl(char *); char * seekstr(char *,char *); int chk_ov(char *,char *,char *); void repstr(char *,char *,char *); int main() { char string[SIZ],from[SIZ],to[SIZ],*p; puts("String substitution program\n"); printf("Enter the string: "); fgets(string,SIZ,stdin); printf("Enter the sub-string: "); fgets(from,SIZ,stdin); remnl(string); /*remove any stored newlines - as fgets stores newline from istream*/ remnl(from); /*remove any stored newlines - as fgets stores newline from istream*/ p=seekstr(from,string); /*seek occorance of from in string*/ if(p==NULL) /*NULL returned - string not found*/ { printf("\nString not found!\n"); exit(1); /*exit with error code 1 on not found*/ } /*control reaches here means string found*/ printf("Enter the substitution sub-string: "); fgets(to,SIZ,stdin); remnl(to); /*remove any stored newlines - as fgets stores newline from istream*/ if(chk_ov(string,from,to)) /*pre-determine if string overflows due to substitution: 1 for ov and 0 otherwise*/ { printf("\nString overflow!\n"); exit(2); /*exit with error code 2 on overflow*/ } /*control reaches here means no overflow pre-determined*/ repstr(p,from,to); /*replace occorance of from with to*/ puts("------------"); printf("\nResult: "); puts(string); /*print result*/ return 0; /*exit with success: error code 0*/ } void repstr(char *str,char *from,char *to) { char *tmp=(char *)malloc(sizeof(char)*strlen(str)+1); /*allocate only required amount of memory for temporary array*/ strcpy(tmp,(char *)(str+strlen(from)));/*copy remainder of sting str to tmp starting from ahead of end of occorance of from in it*/ strcpy(str,to); /*overwrite str with null-terminated to*/ strcat(str,tmp); /*concatenate temporarily stored remainder of str with str*/ return; } char * seekstr(char *substr,char *str) { int iter; char *tmp=(char *)malloc(sizeof(char)*strlen(str)-strlen(substr)+1); /*allocate only required amount of memory for temporary array*/ char *p; for(iter=strlen(str)-strlen(substr),p=str;iter>=0;iter--,p++) /*iterations continue till iter becomes negative*/ { strncpy(tmp,p,strlen(substr)); /*copy strlen(substr) characters from position p to tmp*/ *(char *)(tmp+strlen(substr))='\0'; /*as strncpy does not null terminate the string*/ if(strcmp(substr,tmp)==0) return p; /*return point of occorance of substr in str*/ } return NULL; } void remnl(char *str) { if(*(char *)(str+strlen(str)-1)=='\n') /*if newline is encountered before null character, substitute it with null*/ *(char *)(str+strlen(str)-1)='\0'; return; } int chk_ov(char *str,char *from,char *to) { if(strlen(str)-strlen(from)+strlen(to)>SIZ-1||strlen(from)>strlen(str)) /*pre-determine if overflow occors after substitution*/ return 1; /*return 1 if ov occors*/ return 0; /*return 0 if no ov occors*/ }