The C Programming Language Exercise

1-9 :

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 

point : change the entry of space putchar when there has met a space already.

#include <stdio.h>
main(){
    int c = 0;
    int inspace = 0;
    while((c = getchar()) != EOF){
        if(c == ‘ ‘){
            if(inspace == 0){
                putchar(c);
                inspace = 1;
            }
        }
        else{
            inspace = 0;
            putchar(c);
        }
    }
}

count the numbers of word:

 1 #include <stdio.h>
 2 #define IN 1  //no ";"
 3 #define OUT 0
 4
 5 main(){
 6     int c ;
 7     int state = OUT;
 8     int nc, nw, nl;
 9     nc = nw = nl = 0;  //注意初始化
10
11     while((c = getchar()) != EOF){
12         nc ++;
13         if(c == ‘\t‘)
14             nl++;
15         if(c == ‘ ‘ || c == ‘\t‘ || c == ‘\n‘) {
16             state = OUT;
17         }
18         else if(state == OUT){  //if state is OUT here, means in the word, chage state and ++nw
19             state = IN;
20             nw++;
21         }
22     }
23     printf("%d %d %d",nc, nw, nl);
24 }

design a program output the result that every line contains only a word

 1 #include <stdio.h>
 2 #define IN 1  //no ";"
 3 #define OUT 0
 4
 5 main(){
 6     int c ;
 7     int inspace = 0;
 8     while((c = getchar()) != EOF){
 9         if(c == ‘ ‘ || c == ‘\t‘ || c == ‘\t‘){
10             if(inspace == 0){
11                 inspace = 1;
12                 putchar(‘\n‘);
13             }
14         }
15         else{
16             inspace = 0;
17             putchar(c);
18         }
19     }
20     return 0;
21 }
时间: 2024-10-11 06:41:58

The C Programming Language Exercise的相关文章

The C Programming Language——Exercise solutions of the chapter one (1st)

Recently,I have been reading the book The C Progrmming Lanuage(2nd Edition),which written by Dennis M.Ritchie and Brian W.Kernighan.The are many useful and elegant exercises in the book.Here is my solutions to the exercises of the first chapter but n

Go语言自述(The Go Programming Language README)

声明:本文为笔者为练习英语所做的翻译练习,原文所属者与笔者没有任何关系,翻译结果不代表原文所属者的观点.笔者不保证翻译的正确性,任何人以任何形式的对本文的引用,都是不负责任和荒谬的行为,造成的后果笔者不予负责. 原文链接所属:golang/go Go is an open source programming language that makes it easy to build simple,reliable, and efficient software. Go是一门开源的编程语言,用它可

转:从《The C Programming Language》中学到的那些编程风格和设计思想

这儿有一篇写的很好的读后感:http://www.cnblogs.com/xkfz007/articles/2566424.html 读书不是目的,关键在于思考. 很早就在水木上看到有人推荐<The C Programming Language>这本书,一直都没看,开学一个月就专心拜读了一下,并认真做了课后习题.读来收获不少,主要有两点:一是加深了自己对一些基础知识的理 解和感悟:二是从中学到了一些不错的编程风格和设计思想,这些东西虽看起来不起眼但细细嚼来还是很值得学习的.下面就从四个方面做一

The Swift Programming Language (基础部分)

简介 Swift 是一门开发 iOS, OS X 和 watchOS 应用的新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的. Swift 包含了 C 和 Objective-C 上所有基础数据类型,Int表示整型值:Double和Float表示浮点型值:Bool是布尔型值:String是文本型数据.Swift 还提供了三个基本的集合类型,Array,Set和Dictionary,详见集合类型. 就像 C 语言一样,Swift

《The Swift Programming Language》的笔记-第24页

The Swift Programming Language读书笔记学习笔记 第24页 本页主要内容有两个:打印输出和如何在swift注释代码 1 如何打印变量和常量的值? 使用println函数,细节:println函数除打印变量常量值外还输出一个换行 1) 打印常量 eg1 let hw = "hello chinagame.me" println(hw) => 可将字符串常量输出到Console里 2) 打印变量, 双引号里用 \(变量名) 输出,这个像C语言里的%s eg

[iOS翻译]《The Swift Programming Language》系列:Welcome to Swift-01

本文转载至:http://www.cnblogs.com/yangfaxian/p/3765081.html 全书目录: 一.Welcome to Swift 二.Language Guide 三.Language Reference /* 译者的废话: 几个小时前熬夜看了WWDC,各种激动,今年很有料啊!当看到Swift出来的时候,瞬间傻眼,又要学习新语言了.这篇文章来自苹果官方的<The Swift Programming Language>一书,500页左右,在苹果官网有下载.Swift

Questions that are independent of programming language. These questions are typically more abstract than other categories.

Questions that are independent of programming language.  These questions are typically more abstract than other categories. Free Language Agnostic Programming Books 97 Things Every Programmer Should Know Algorithms and Data-Structures (PDF) Algorithm

The Swift Programming Language 中文版

iSwifting社区 Swift 兴趣交流群:303868520 iOS 微信公众账号:iOSDevTip Swift 微信公众账号:SwiftDev iSwifting社区 如果你觉得这个项目不错,请点击Star一下,您的支持我们最大的动力. The Swift Programming Language 中文版 这一次,让中国和世界同步 The Swift Programming Language 中文版,布布扣,bubuko.com

The Swift Programming Language IOS8 快捷编程语言

This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This info