Sunday, January 12, 2014

find and replace program UIC


You are to submit the programs for this lab via the Assignments Page in Blackboard.
To help the TA, name your file with your net-id and the assignment name, like:
• ptroy1LabX.c
Examples for the Project
Assume the input file contains the following:
#h#j#
hello
happy birthday
#ra#ar#
radar
are you ready
pirate radio rating
##This is an Error – Target String has length of zero#
this line should still use the target string of ra
###
The above begins with one target/replacement pair followed by two original strings, then has a
second target/replacement pair followed by 3 strings. The first target/replacement pair tells your
program to replace all occurrences of h in an original string with a j. Thus the first original string
of hello becomes jello and the second original string of happy birthday becomes jappy
birtjday. The second target/replacement pair tells your program to replace all occurrences of ra
with ar. Thus the original string of radar becomes ardar, the original string of are you ready
becomes are you ready (no replacements are done since the target string does not exist), and the
original string of pirate radio rating becomes pirate ardio arting.

" Code "
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
char *separate (char Origin[MAX], char subw1[MAX], char subw2[MAX])
{
char *remove;
char *look = "#";
remove = strtok (Origin,look);
strcpy (subw1, remove);
remove = strtok(NULL,look);
strcpy (subw2, remove);
remove = strtok(NULL,look);
return subw1, subw2;
}
char* str_replace(const char *buff, const char *stringOriginal, const char *stringNew) {
char *strret, *p = NULL;
char *posnews, *posold;
size_t szold = strlen(stringOriginal);
size_t sznew = strlen(stringNew);
size_t n = 1;
if(!buff)
return NULL;
if(!stringOriginal || !stringNew || !(p = strstr(buff, stringOriginal)))
return strdup(buff);
while(n > 0) {
if(!(p = strstr(p+1, stringOriginal)))
break;
n++;
}
strret = (char*)malloc(strlen(buff)-(n*szold)+(n*sznew)+1);
p = strstr(buff, stringOriginal);
strncpy(strret, buff, (p-buff));
strret[p-buff] = 0;
posold = p+szold;
posnews = strret+(p-buff);
strcpy(posnews, stringNew);
posnews += sznew;
while(n > 0) {
if(!(p = strstr(posold, stringOriginal)))
break;
strncpy(posnews, posold, p-posold);
posnews[p-posold] = 0;
posnews += (p-posold);
strcpy(posnews, stringNew);
posnews += sznew;
posold = p+szold;
}
strcpy(posnews, posold);
return strret;
}
int main(int argc, char **argv) {
char input_string[MAX];
char word_original[MAX];
char word_to_replace[MAX];
char word_to_compare[MAX];
int notDone =0;
printf("please enter your # sign follow by your original Word # and your replacement Word then # to enclose\n");
do{
fgets(input_string,sizeof(input_string),stdin);   //get user input for string
if(input_string[0]=='#')
goto there;
else if(input_string[0]!='#'){
goto here;
}
else if(input_string[0]=='#' && input_string[1]=='#')
{
printf("There is no string to replace\n");
goto here;
}
else if(input_string =="###"){
printf("### in a row Exit.\n");
notDone =1;
return 0;
}
there:
printf("you enter =>                %s" ,input_string);
separate(input_string, word_original, word_to_replace);
fgets(word_to_compare,sizeof(word_to_compare),stdin);
printf("original string     is =>   %s\n",word_to_compare);
printf("string after chaged is =>   %s\n", str_replace(word_to_compare,word_original,word_to_replace ));
fgets(input_string,sizeof(input_string),stdin);
if(input_string[0]=='#'&& input_string[1]=='#')
{
printf("There is no string to replace\n");
}
else if(input_string[0]=='#' && input_string[1]=='#')
{
printf("There is no string to replace\n");
goto here;
}
else if(input_string[0]=='#'){
goto there;
}
else if(input_string[0]!='#'){
goto here;
}
here:
printf("original string      is=>  %s\n",input_string);
printf("string after changed is=>  %s\n", str_replace(input_string,word_original,word_to_replace ));
}while(notDone==0);
return 0;
}


No comments:

Post a Comment