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
|
#include <stdio.h>
/*
Exercise 2-4. Write an alternate version of squeeze (s 1,s2) that deletes
each character in s1 that matches any character in the string s2.
*/
/*
//squeeze: delete all c from s (needs below lines to work....)
void squeeze(char s[], char c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; ++i)
{
if (s[i] != c)
{
s[j++] = s[i];
}
s[j] = '\0';
}
}
//int i;
//strip null chars that are just there in the middle of the string... otherwise printf does not work
//for (i=0;i<5;++i)
//{
//if (input[i] == '\0'){input[i] = input[i+1]; input[i+1] = '\0'; }
//}
*/
//squeeze: Actual working reimplementation
void squeeze(char s[], char c)
{
int i,j;
for (i=0; s[i] != '\0'; ++i)
{
if (s[i] == c)//if s[i] is a charater to remove....
{
j=i;
while(s[j] != '\0'){s[j++] = s[j+1];}//shift all characters above left, overwriting the character
--i;//deincrement i, since the string was just shrunk by one and the one just above could indeed be a matching char
}
}
}
//squeezy: Delete all chars in s0 that match each single char in s1
void squeezy(char s0[], char s1[])
{
int i,j,z;
for (i=0; s0[i] != '\0'; ++i)
{
for (z=0; s1[z] != '\0'; ++z)
{
if (s0[i] == s1[z])//if s0[i] is a charater to remove....
{
j=i;
while(s0[j] != '\0'){s0[j++] = s0[j+1];}//shift all characters above left, overwriting the character
--i;//deincrement i, since the string was just shrunk by one and the one just above could indeed be a matching char
}
}
}
}
int main(void)
{
//note: "char *" cannot be modified like; s[0] = 'a', char s[] needs to be used instead
char input[] = "Test input";
//squeeze(input,'e');
squeezy(input,"t");
printf("%s\n", input);
return 0;
}
|