Skip to content

Instantly share code, notes, and snippets.

/*
* Exercise 2-4. Write an alternative version of squeeze(s1,s2) that deletes
* each character in s1 that matches any character in the string s2.
*/
#include <stdio.h>
#include <stdlib.h>
void squeeze(char s1[], const char s2[]);
int indexof(const char s[], char c);
/*
* Exercise 2-5. Write the function any(s1,s2), which returns the first
* location in a string s1 where any character from the string s2 occurs,
* or -1 if s1 contains no characters from s2. (The standard library
* function strpbrk does the same job but returns a pointer to the location.)
*/
#include <stdio.h>
int any(const char s1[], const char s2[]);
/*
* Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits
* that begin at position p set to the rightmost n bits of y, leaving the other
* bits unchanged.
*/
#include <stdio.h>
unsigned int setbits(unsigned int x, int p, int n, unsigned int y);
unsigned int setbits(unsigned int x, int p, int n, unsigned int y) {
/*
* Exercise 2-7. Write a function invert(x,p,n) that returns x with the n
* bits that begin at position p inverted (i.e., 1 changed into 0 and vice
* versa), leaving the others unchanged.
*/
#include <stdio.h>
unsigned int invert(unsigned int x, int p, int n);
unsigned int invert(unsigned int x, int p, int n) {
/*
* Exercise 2-9. In a two's complement number system, x &= (x-1) deletes the
* right most 1-bit in x. Explain why. Use this observation to write a faster
* version of bitcount.
*/
#include <stdio.h>
int bitcount(unsigned int x);
int bitcount(unsigned int x) {
/*
* Exercise 2-8. Write a function rightrot(x,n) that returns the value of the
* integer x rotated to the right by n positions.
*/
#include <stdio.h>
unsigned int rightrot(unsigned int x, int n);
unsigned int rightrot(unsigned int x, int n) {
while (n > 0) {
/*
* Exercise 3-2. Write a function escape(s,t) that converts characters like
* newline and tab into visible escape sequences like \n and \t as it copies
* the string t to s. Use a switch. Write a function for the other direction
* as well, converting escape sequences into the real characters.
*/
#include <stdio.h>
#define MAXLEN 1000
int escape(char s[], const char t[]);
#include <ctype.h>
/*
* 将十进制字符串转换为数值。可处理前导的 + 和 -
*
* 如果有空白符,则跳过
* 如果有符号,则读取符号
* 取整数部分,并执行转换
* 当遇到第一个不能转换为数字的字符时,整个处理过程终止
*/
/*
* Exercise 3-3. Write a function expand(s1,s2) that expands shorthand
* notations like a-z in the string s1 into the equivalent complete list
* abc...xyz in s2. Allow for letters of either case and digits, and be
* prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arrange that
* a leading or trailing - is taken literally.
*/
#include <stdio.h>
/*
* Exercise 3-4. In a two's complement number representation, our version of
* itoa does not handle the largest negative number, that is, the value of n
* equal to -(2^wordsize - 1). Explain why not. Modify it to print that value
* correctly, regardless of the machine on which it runs.
*/
#include <stdio.h>
#include <limits.h>
#include <string.h>