-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_replace.c
150 lines (113 loc) · 4.48 KB
/
string_replace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Name: Yuvashree
*
A program in C that replaces every occurrence of a given pattern in every line of a given text file with a sequence of asterisks of the same length as the pattern .
The output generated by the program is be sent to the standard output.
* Bug report: “No bugs, and all the features have been implemented” */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[]){
//initializations
FILE *fp;
int count=0;
char programName[20];
strcpy(programName, argv[0]);
char caseSensitive[2];
strcpy(caseSensitive, argv[1]);
int noOfCommands = argc;
char stringToBeReplaced[15];
int casesense;
//Usage feature check
if( noOfCommands==4 && (strcmp(caseSensitive,"-i")==0) ){
fp = fopen(argv[2],"r");
casesense = 0;
strcpy(stringToBeReplaced, argv[3]);
}
else if ( noOfCommands == 3 ){
fp = fopen(argv[1],"r");
casesense = 1;
strcpy(stringToBeReplaced, argv[2]);
}
else {
printf("Usage: %s [-i] <filename> <pattern> \n",programName);
exit(1);
}
//checking pattern length
while(stringToBeReplaced[count]!='\0'){
count+=1;
if(count>=15){
printf("please give pattern to be replaced below 15 characters\n");
exit(1);
}
}
//checking file
if(fp==NULL){
fprintf(stderr,"Error opening file\n " );
exit(1);
}
//calling function for replacing string with *
stringReplace(fp,casesense,stringToBeReplaced);
fclose(fp);
return(0);
}
int stringReplace(FILE *filep, int casesense, char stringToBeReplaced[15]){
char input[301];
int j;
int sizeOfStr = strlen(stringToBeReplaced);
int incr = 0;
char ash[sizeOfStr];
//creating array of *
for (incr;incr<sizeOfStr;incr++){
ash[incr]='*';
}
//loop for wrinting file in input (array of char)
while(!feof(filep)){
int i=0;
memset(input, 0, sizeof(input));
fgets(input,300,filep);
//loop exetutes till end of input
while(input[i]!='\0'){
int size = strlen(stringToBeReplaced);
j=0;
char temp[size];
memset(temp, 0, sizeof(temp));
//compare each char wtih first letter of given pattern
//if there is a match next char is compared
if( (casesense==1 && stringToBeReplaced[j]==input[i]) || (casesense==0 && tolower( stringToBeReplaced[j])==tolower(input[i])) ){
int tempcount = 0;
while(size>=1){
if(size==1){
printf(ash);
i+=1;
break;
}
temp[tempcount]=input[i];
tempcount += 1;
i+=1;
j+=1;
size-=1;
//compared remaining char
// if all char matches with pattern, * is printed
if((casesense==1 && stringToBeReplaced[j]==input[i]) || (casesense==0 && tolower(stringToBeReplaced[j])==tolower(input[i]))){
if(size==1){
printf(ash);
i+=1;
break;
}
}
else{
printf(temp);
size=0;
break;
}
}
}
else{
printf("%c",input[i]);
i+=1;
}
}
}
return(0);
}