How do I complete K&R Exercise 2-4?

前端 未结 5 547
Happy的楠姐
Happy的楠姐 2020-12-18 14:49

I\'m learning how to write programs in C using the k&r book (The C Programming Language) and I have a problem with one of the exercises. It\'s asking me to detect and re

5条回答
  •  心在旅途
    2020-12-18 15:32

    Great book. If I were you, I would proceed exactly as for the squeeze() in section 2.8, but instead of a direct comparison (s[i] != c) I would write and exploit a function

     int contains(char s[], int c)
    

    which returns 1 if the string s contains c, 0 otherwise. Start with the simple approach; when it works you may improve performance with more complex solutions (binary search, but note that the problem doesn't require the characters in s2 to be in a particular order).

提交回复
热议问题