1、编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行

  1. #include<stdio.h>
  2. #define MAXLINE 1000 // maximum input line size
  3. int getline(char line[], int maxline);
  4. int delete(char s[]);
  5. // remove trailing blanks and tabs, and delete blank lines
  6. int main(void)
  7. {
  8. char line[MAXLINE]; // current input line
  9.  
  10. while(getline(line, MAXLINE) > 0)
  11. if(delete(line) > 0)
  12. printf("%s", line);
  13.  
  14. return 0;
  15. }
  16.  
  17. // getline: read a line into s, return length
  18. int getline(char s[], int lim)
  19. {
  20. int c, i, j; // i记录字符串的长度,j记录被复制到字符串中的字符的个数
  21.  
  22. j = 0;
  23. for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
  24. if(i < lim-2)
  25. {
  26. s[j] = c; // line still in boundaries
  27. ++j;
  28. }
  29. if(c == '\n')
  30. {
  31. s[j] = c;
  32. ++j;
  33. ++i;
  34. }
  35. s[j] = '\0';
  36. return i;
  37. }
  38. // remove trailing blanks and tabs from character string s
  39. int delete(char s[])
  40. {
  41. int i;
  42.  
  43. i = 0;
  44. while(s[i] != '\n') // find newline character
  45. ++i;
  46. --i; // back off from '\n'
  47. while(i > 0 && (s[i] == ' ' || s[i] == '\t'))
  48. --i;
  49. if(i >= 0) // is it a nonblank line?
  50. {
  51. ++i;
  52. s[i] = '\n'; // put newline character back
  53. ++i;
  54. s[i] = '\0'; // terminate the string
  55. }
  56. return i;
  57. }

2、编写一个翻转字符串顺序的函数,使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序

  1. #include<stdio.h>
  2. #define MAXLINE 1000 // maximum input line size
  3. int getline(char line[], int maxline);
  4. void reverse(char s[]);
  5. // reverse input lines, a line at a time
  6. int main(void)
  7. {
  8. char line[MAXLINE]; // current input line
  9.  
  10. while(getline(line, MAXLINE) > 0)
  11. {
  12. reverse(line);
  13. printf("%s", line);
  14. }
  15. return 0;
  16. }
  17. // getline: read a line into s, return length
  18. int getline(char s[], int lim)
  19. {
  20. int c, i, j; // i记录字符串长度,j记录被复制到字符串s中的字符的个数
  21.  
  22. j = 0;
  23. for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
  24. {
  25. if(i < lim-2)
  26. {
  27. s[j] = c; // line still in boundaries
  28. ++j;
  29. }
  30. }
  31. if(c == '\n')
  32. {
  33. s[j] = c;
  34. ++j;
  35. ++i;
  36. }
  37. s[j] = '\0';
  38. return i;
  39. }
  40. // reverse: reverse string s
  41. void reverse(char s[])
  42. {
  43. int i, j;
  44. char temp;
  45.  
  46. i = 0;
  47. while(s[i] != '\0') // find the end of string s
  48. ++i;
  49. --i; // back off from '\0'
  50. if(s[i] == '\n')
  51. --i; // leave newline in place
  52. j = 0;
  53. while(j < i) // beginning of new strings
  54. {
  55. temp = s[j];
  56. s[j] = s[i]; // swap the characters
  57. s[i] = temp;
  58. --i;
  59. ++j;
  60. }
  61. }

3、编写程序,将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止的地方,假设制表符终止的位置是固定的,比如每隔n 列就会出现一个制表符终止位,n 应作为变量还是符号常量呢?

  1. #include<stdio.h>
  2. #define TABINC 8 // tab increment size
  3. // replace tabs with the proper number of blanks
  4. int main(void)
  5. {
  6. int c, nb, pos;
  7.  
  8. nb = 0; // number of blanks necessary
  9. pos = 1; // position of character in line
  10. while((c = getchar()) != EOF)
  11. {
  12. if(c == '\t') // tab character
  13. {
  14. nb = TABINC - (pos-1) % TABINC;
  15. while(nb > 0)
  16. {
  17. putchar(' ');
  18. ++pos;
  19. --nb;
  20. }
  21. }
  22. else if(c == '\n') // newline character
  23. {
  24. putchar(c);
  25. pos = 1;
  26. }
  27. else
  28. {
  29. putchar(c); // all other characters
  30. ++pos;
  31. }
  32. }
  33.  
  34. return 0;
  35. }

Getting started with the basics of programming exercises_3的更多相关文章

  1. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  2. Getting started with the basics of programming exercises_4

    1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...

  3. Getting started with the basics of programming exercises_2

    1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...

  4. Getting started with the basics of programming exercises_1

    1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...

  5. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  6. C语言学习书籍推荐《Practical C++ Programming》下载

    下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...

  7. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  8. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  9. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

随机推荐

  1. springmvc下载一个文档下载接口里的文档

    A提供了一个文件下载的接口,在调用的时候可以直接在前端用a标签来调用 <a href="http://" target="_blank">下载< ...

  2. Uva437 The Tower of Babylon

    https://odzkskevi.qnssl.com/5e1fdf8cae5d11a8f572bae96d6095c0?v=1507521965 Perhaps you have heard of ...

  3. 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线

    P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...

  4. 【模板】KMP [2017年5月计划 清北学堂51精英班Day2]

    Day2就搞一个KMP把  马拉车.AC自动机等准备省选的时候再说.. 模板题: 1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze     ...

  5. python 从数据库取回来的数据中文显示为乱码

    问题:从数据库取回来的数据,中文显示为乱码. 解决办法: 此处要指定charset为utf-8(一般数据库编码都是utf8),否则读取出的中文会乱码

  6. 微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应

    wxml: <image style="width: {{imagewidth}}px; height: {{imageheight}}px;"  src="{{i ...

  7. 创建一个User类

    1.用户模型—User类 用户模型或者叫账户模型,为什么这么说看下面代码 using System; using System.ComponentModel.DataAnnotations; name ...

  8. Eclipse Jobs 和后台进程

    Eclipse后台进程 1.主线程(Main thread) 一个Eclipse客户端只能跑一个进程,但却可以创建很多线程. 在Eclipse框架中,会用一个单线程去运行所有的代码指令,这个线程执行客 ...

  9. Linux 内存管理之mmap详解

    找了好多,最后发现下面这篇时讲的比较通俗易懂的. Linux内存管理之mmap详解-heavent2010-ChinaUnix博客 http://blog.chinaunix.net/uid-2666 ...

  10. 【JZOJ4799】【NOIP2016提高A组模拟9.24】我的快乐时代

    题目描述 输入 一行,两个整数l,r . 输出 一行,一个整数,表示第l 天到第r 天的愉悦值的和. 样例输入 64 89 样例输出 1818 数据范围 解法 可以参考数位动态规划的想法. 从个位开始 ...