-
Notifications
You must be signed in to change notification settings - Fork 0
/
automatedrecieptgenerator.cpp
72 lines (62 loc) · 1.81 KB
/
automatedrecieptgenerator.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// this is a string
char * replaceWord(const char * str, const char * oldWord, const char * newWord)
{
char * resultString;
int i, count = 0;
int newWordLength = strlen(newWord);
int oldWordLength = strlen(oldWord);
// Lets count the number of times old word occurs in the string
for (i = 0; str[i] !='\0'; i++)
{
if (strstr(&str[i], oldWord) == &str[i])
{
count++;
// Jump over this word
i = i + oldWordLength - 1;
}
}
// Making a new string to fit in the replaced words
resultString = (char *)malloc(i + count * (newWordLength - oldWordLength) + 1);
i = 0;
while (*str)
{
// Compare the substring with result
if(strstr(str, oldWord) == str)
{
strcpy(&resultString[i], newWord);
i += newWordLength;
str += oldWordLength;
}
else{
resultString[i] = *str;
i += 1;
str +=1;
}
}
resultString[i] = '\0';
return resultString;
}
int main()
{
FILE * ptr = NULL;
FILE * ptr2 = NULL;
ptr = fopen("bill.txt", "r");
ptr2 = fopen("genBill.txt", "w");
char str [200];
fgets(str, 200, ptr);
printf("The given bill template was: %s\n", str);
// Call the replaceWord function and generate newStr
char * newStr;
newStr = replaceWord(str, "{{item}}", "Table Fan");
newStr = replaceWord(newStr, "{{outlet}}", "Ram Laxmi fan outlet");
newStr = replaceWord(newStr, "{{name}}", "Harry");
printf("The actual bill generated is: %s\n", newStr);
printf("The generated bill has been written to the file genBill.txt\n");
fprintf(ptr2, "%s", newStr);
fclose(ptr);
fclose(ptr2);
return 0;
}