summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGentoo <installgentoo@endianness.com>2021-03-27 10:17:46 +1100
committerGentoo <installgentoo@endianness.com>2021-03-27 10:17:46 +1100
commitb5ccfb3c40502c633855598029dd727d3b743720 (patch)
tree69dc3f4b9fd921ff99503af373d4f5759bfd084b /main.c
downloadsqueeze-master.tar.gz
squeeze-master.tar.bz2
squeeze-master.zip
initial commitHEADmaster
Diffstat (limited to 'main.c')
-rw-r--r--main.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..890b8bd
--- /dev/null
+++ b/main.c
@@ -0,0 +1,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;
+}
+