kernighan-and-ritchie

K & R Exercise: My Code Works, But Feels Stinky; Advice for Cleanup?

I'm working on the K&R book. I've read farther ahead than I've done exercises, mostly for lack of time. I'm catching up, and have done almost all the exercises from chapter 1, which is the tutorial. My issue was exercise 1-18. The exercise is to: Write a program to remove trailing blanks and tabs from line of input, and to delete...

K&R Exercise 2-4

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 remove a character in string s1, which matches any characters in the string s2. So, say s1 = "A"; And s2 = "AABAACAADAAE" I want it to return "BCDE" I know I'm on the rig...

Beginner Doing K&R

Hi, I'm just starting programming and going through K&R to try and learn C. I've gotten to the section on command line arguments (5.10) but now I'm stumped. Every time I try and open a program I've written with command line arguments I'm told that file X, X being the argument, doesn't exist. `gcc -o find find.c open find test The f...

Help With K&Rs Counting Chars Example

I'm working my way through K&R's 2nd edition, and I've been stumped with this seemingly simple example: #include <stdio.h> main(){ double c; for(c = 0; ((getchar() != EOF) && (getchar() != '\n')); ++c) ; printf("%.0f\n",c); } It simply isn't working correctly. I added in the (getchar() != '\n') portion to end the...

"The C Programming Language" interesting quote in the preface

From the preface of the second edition of Kernighan and Ritchie's "The C Programming Language": As before, all examples have been tested directly from the text, which is in machine-readable form. That quote threw me for a loop. What exactly does it mean? Was the original manuscript written as a literate program? My first thought wa...

Is K&R teaching bad readability?

Hello. It has been a while since I looked at C (still learning) and I just got back into the K&R book. I just had a go to Exercise 5-3 (p107). Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s. I came up with this... void strcat(char *s, char *t); void st...

In C, there is a way to know when the end of string is reached. Is there a way to know the start when iterating backwards?

Hi. I'm doing K&R Exercise 5-4 (p107). Write the function strend(s,t) , which returns 1 if the string t occurs at the end of the string s, and zero otherwise. I figured the best way to do this was to... increment both pointers to the end count backwards through both strings whilst each char matches return 1 if we had finished c...