K&R C Exercise 3-2: escape — Convert Special Characters to Escape Sequences

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. Two functions walking their input strings character …

K&R C Exercise 2-10: lower — Convert to Lowercase with Ternary

Exercise 2-10. Rewrite the function lower, which converts upper case letters to lower case, with a conditional expression instead of if-else. This exercise looks trivial on the surface — it is a one-liner. The real lesson is about the conditional (ternary) operator and what it means to choose it over if-else. K&R introduce the ternary …

K&R C Exercise 2-8: rightrot — Rotate Bits Right

K&R C Exercise 2-8 — rightrot(x, n) Exercise 2-8: Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions. Approach A right rotation is different from a right shift. When you shift right by n, the n bits that fall off the right end are …

C Program to add two polynomials using structures.

C Program to add two polynomials using structures. Structure is a c composite data type, in which we can define all the data types under the same name or object. Size of the Structure is the size of all data types, plus any internal padding. the key word “struct” is used to declare the structure. …

K&R C Exercise 2-6: setbits — Set n Bits at Position p

K&R C Exercise 2-6 — setbits(x, p, n, y) 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. Approach K&R numbers bit positions from the right starting at 0, so bit 0 is …

K&R C Exercise 2-3: htoi — Convert Hex String to Integer

Exercise 2-3. Write the function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F. How htoi Works Hexadecimal is simply base-16 positional notation. Converting from a hex string to an integer …