-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_strings.c
More file actions
181 lines (151 loc) · 3.61 KB
/
my_strings.c
File metadata and controls
181 lines (151 loc) · 3.61 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
*
* File: my_strings.c
* Author: naomi lambert
*
*
*/
#include "my_strings.h"
#define MAX_STR 20
/*
* length: Returns the length of a string.
*
* Parameters:
* string[] - string
*
* Returns: length of string string[].
*/
int length(const char string[])
{
int count = 0;
const char *p = string;
if ( p != NULL) {
while (*(p+count) != '\0'){
count++;
}
}
return count;
}
/*
* reverse: Reverses a string s. For example, if outputting s
* produces "Hello" before the function, then outputting
* s after the function produces "olleH".
*
* Parameters:
* string[] - string
*
* Modifies: string[] so that the original string is stored in reverse order.
*/
void reverse(char string[])
{
int len = length(string), j = len - 1, i = 0;
char temp;
while ( i < j) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
i++;
j--;
}
}
/*
* insert: Inserts s2 into s1 at the nth character of s1. The size
* of s1[] array is assumed to be large enough to store s1 + s2.
* For an n == 0, s2 precedes s1. For n >= length-of-s1, s2 follows s1.
*
* Parameters:
* s1[] - string
* s2[] - string
* n - integer
*
* Modifies: s1[] so that it contains the original s1, additionally
* with s2 inserted at the nth location in s1.
*/
void insert(char s1[], const char s2[], int n)
{
/* insert s2 into s1 immediately following the nth character
* e.g. parameters @Alice and Bob, @Alice, @5 should result in
* 'AliceAlice and Bob'
* */
int i, j, k, q;
int len_s2 = length(s2);
int len_s1 = length(s1);
if (n < len_s1){
int other_chars_len = len_s1 - n;
char other_chars[other_chars_len];
for (i = 0; i < other_chars_len; i++){
other_chars[i] = s1[n+i];
}
for (j = 0; j < len_s2; j++) {
s1[n+j] = s2[j];
}
for (k = 0; k < other_chars_len; k++){
s1[n+len_s2+k] = other_chars[k];
}
} else {
for (q = 0; q < len_s2; q++)
s1[len_s1+q] = s2[q];
}
}
/*
* search: Determines if string s2 appears as a substring in s1.
*
* Parameters:
* s1[] - string
* s2[] - string
*
* Returns:
* true - if s2 is in s1;
* false - otherwise.
*/
bool search(const char s1[], const char s2[])
{
int len_s2 = length(s2);
int len_s1 = length(s1);
char temp[len_s2];
int i = 0, j, k;
bool match = true;
if (len_s2 > len_s1){
return false;
} else {
while (i <= len_s1 - len_s2) {
for (j = 0; j < len_s2; j++) {
temp[j] = s2[i+j];
}
i++;
}
for (k = 0; k < len_s2; k++){
if (temp[k] != s2[k]){
match = false;
}
}
}
if (match)
return true;
else
return false;
}
/*
* main_strings: This is the entry point to the strings test.
* Samples have been provided.
*
*/
int main_strings()
{
printf("\nLength of \"Hello!\" is %d.\n", length("Hello!"));
char str[MAX_STR + 1] = "Hello!";
printf("The reverse of %s", str);
reverse(str);
printf(" is %s.\n", str);
char ab[MAX_STR + 1] = "Alice and Bob";
char b[MAX_STR + 1] = "Alice";
if(search(ab, b) == true) {
printf("String '%s' is in '%s'!\n", b, ab);
} else {
printf("String '%s' is NOT in '%s'!\n", b, ab);
}
printf("Combine strings: '%s' and '%s'.\n", ab, b);
insert(ab, b, 5);
printf("Result is: '%s'.\n", ab);
return 0;
}