-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.c
155 lines (138 loc) · 3.95 KB
/
lab2.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
151
152
153
154
155
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
#define TRUE 1
#define FALSE 0
void findFirstOccurance(char text[], char word[]);
void findMultipleOccurances(char text[], char word[]);
void printTable(int table[], int size);
void stringCopy(char text1[], char text2[]);
void stringConcatenation(char text1[], char text2[]);
void stringComparison(char text1[], char text2[]);
void bubbleSort(int tab[], int size);
void pointerSearch(int tab[], int size, int num);
int main() {
// TODO
// Funkcje z biblioteki <string.h>
// --> strlen(), done
// --> strcat(), done
// --> strcmp(), done
// --> strncmp(), ?
// --> strstr(), ?
// --> strcpy(), done
}
void findFirstOccurance(char text[], char word[]) {
int found, i=0, j=0;
while (text[i] != '\0') {
if (text[i] == word[0]) {
found = TRUE;
while (word[j] != '\0') {
if (text[i+j] != word[j]) {
found = FALSE;
break;
}
j++;
}
}
if (found == TRUE) break;
i++;
}
if (found == TRUE) printf("Found it at index %d!", i);
}
void findMultipleOccurances(char text[], char word[]) {
int i=0, j=1, found, counter=0;
int indexes[MAX_SIZE];
while (text[i] != '\0') {
found = FALSE;
if (text[i] == word[0]) {
found = TRUE;
while (word[j] != '\0') {
if (word[j] != text[i+j]) {
found = FALSE;
break;
}
j++;
}
}
if (found == TRUE) indexes[counter++] = i;
i++;
}
if (counter != 0) {
printf("Word you were looking for occurs %d times, at index: ", counter);
printTable(indexes, counter);
}
}
void printTable(int table[], int size) {
for (int i=0; i < size; i++) {
printf("%d ", table[i]);
}
}
void stringCopy(char text1[], char text2[]) {
int len1 = (int) strlen(text1);
int len2 = (int) strlen(text2);
for (int i=0; i < len1; i++) {
text2[i] = text1[i];
printf("%c", text2[i]);
}
}
void stringConcatenation(char text1[], char text2[]) {
int len1 = (int) strlen(text1);
int len2 = (int) strlen(text2);
if (len1 + len2 > MAX_SIZE) {
printf("Strings are too big - could not perform concatenation ...");
return;
} else {
for (int i=len1; i < len1 + len2; i++) text1[i] = text2[i-len1];
}
}
void stringComparison(char text1[], char text2[]) {
int len1 = (int) strlen(text1);
int len2 = (int) strlen(text2);
if (len1 != len2 || len1==0 || len2==0) {
printf("Texts are not the same.");
return;
} else {
for (size_t i=0; i<len1; i++) {
if (text1[i] != text2[i]) {
printf("Texts are not the same.");
return;
}
}
printf("Texts are the same!");
}
}
void bubbleSort(int tab[], int size) {
// Sorting
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (tab[j] > tab[j + 1]) {
int k = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = k;
}
}
}
}
void pointerSearch(int tab[], int size, int num) {
//create pointers
int a = 0, b = size-1, middle;
//sort table
bubbleSort(tab, size);
//search for number
if (num < tab[0] || num > tab[size-1]) {
printf("Number you are looking for is not in the array.");
} else {
while (a <= b) {
middle = (a+b)/2;
if (num > tab[middle]) {
a = middle+1;
} else if (num < tab[middle]) {
b = middle-1;
} else {
printf("Found it on index %d!", middle);
return;
}
}
printf("Couldn't find the number %d in the aray ...", num);
}
}