-
Notifications
You must be signed in to change notification settings - Fork 21
/
ex7.c
64 lines (52 loc) · 1.59 KB
/
ex7.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
/*
* 7. Write a function called insertString() to insert one character string
* into another string. The arguments to the function should consist of the
* source string, the string to be inserted, and the and the position in the
* source string where the string is to be inerted. So, the call
*
* insertString (text, "per", 10);
*
* with text as originally defined in the previous exercise, results in the
* character string "per" being inserted inside text, beginning at text[10].
* Therefore, the character string "the wrong person" is stored inside the text
* array after the function returned.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
#define MAXLEN 1000
/* functions */
void insertString(char [], const char [], int);
int stringLength(const char []);
/* insertString: insert string t into string s at index pos */
void insertString(char s[], const char t[], int pos)
{
int i, s_len, offset;
s_len = stringLength(s);
/* check if pos is within the range of s */
if (pos > s_len)
return; // do nothing
/* make room for str by 'moving' characters (including '\0') from the end
* of s to the right by a distance equal to s_len */
offset = stringLength(t);
for (i = s_len + 1; i >= pos; --i)
s[i + offset] = s[i];
/* copy str into s starting at pos */
for (i = 0; t[i] != '\0'; ++i, ++pos)
s[pos] = t[i];
}
/* stringLength: return the length of string s */
int stringLength(const char s[])
{
int i;
for (i = 0; s[i] != '\0'; ++i)
;
return i;
}
int main(void)
{
char text[MAXLEN] = "the wrong son";
insertString(text, "per", 10);
printf("%s\n", text);
return 0;
}