loops - C programming language exercise 2-4: I dont understand the answer -


i saw answer in web , doesn't understand how can work loop. , why mine won't work.the question : write alternate version of squeeze(s1,s2) deletes each character in string s1 matches character in string s2. think should check each char in s1 doesn't match every character in s2. see character in s1, loop through s2 see if matches char in s1 , move on next char in s1. outside loop s1 , inner loop s2. here's code , put in whole code test it. output correct. code function part. don't understand why put s2 in outside loop , works.

#include <string.h> #include <stdio.h>  void squeeze2(char s[], char t[]);  void squeeze2(char s[], char t[]) {     int i, j, k;     (k = 0; t[k] != '\0'; k++) {         (i = j = 0; s[i] != '\0'; i++)             if (s[i] != t[k])                 s[j++] = s[i];         s[j] = '\0';     } }  int main() {     char s1[] = "hello meow meow princess";     char s2[] = { 'a', 'e', 'm' };     squeeze2(s1, s2);     int = 0;     while (s1[i] != '\0') {         printf("%c", s1[i]);         i++;     } } 

and theres mine. put s2 in inner code , out put hllo meow meow princess..

#include <string.h> #include <stdio.h>  void squeeze(char s1[],char s2[]);  int main() {     char s1[] = "hello meow meow princess";     char s2[] = { 'a', 'e', 'm' };     squeeze(s1, s2);     int l;     (l = 0; l < strlen(s1); l++)         printf("%c", s1[l]); }  void squeeze(char s1[], char s2[]) {     int = 0;     int k = 0;     while (s1[i] != '\0')         (int j = 0; s2[j] != '\0'; j++) {             if (s1[i] != s2[j]) {                 s1[k] = s1[i];                 k++;             }             i++;        } } 

it not matter whether loop first on s1 , next on s2 or other way around provided adapt actual code.

there problem in both versions: string s2 in main not null terminated (it not c string), behavior in squeeze undefined characters second string dereferenced beyond end of array.

there more problems in version:

  • you not null terminate s1 in case gets shortened if 1 or more of characters found in s2.
  • the algorithm broken: copy each character of s1 many times find character s2 not match it.

you should instead verify if character present in s2, either calling strchr(s2, s1[i]) or enumerating hand.

here corrected , simplified version:

#include <string.h> #include <stdio.h>  void squeeze(char *s1, const char *s2);  int main(void) {     char s1[] = "hello meow meow princess";     char s2[] = { 'a', 'e', 'm', '\0' };     squeeze(s1, s2);     printf("%s\n", s1);     return 0; }  void squeeze(char *s1, const char *s2) {     int i, j, k;     (i = k = 0; s1[i] != '\0'; i++) {         (j = 0; s2[j] != '\0'; j++) {             if (s1[i] == s2[j])                 break;         }         if (s2[j] == '\0') {  // character not found in s2             s1[k++] = s1[i];         }     }     s1[k] = '\0'; // null terminate s1 of shortened } 

notes:

  • always use braces non trivial statement after if, else, for, while , do ... while

  • favor for on while: grouping initialization, increment , test of index variable improves readability , reduces bugs.

  • avoid naming variable l: constant width font, looks similar 1.

  • const qualify string arguments not modified function.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -