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

#include<stdio.h>
#define MAXLINE 1000 // maximum input line size
int getline(char line[], int maxline);
int delete(char s[]);
// remove trailing blanks and tabs, and delete blank lines
int main(void)
{
char line[MAXLINE]; // current input line while(getline(line, MAXLINE) > 0)
if(delete(line) > 0)
printf("%s", line); return 0;
} // getline: read a line into s, return length
int getline(char s[], int lim)
{
int c, i, j; // i记录字符串的长度,j记录被复制到字符串中的字符的个数 j = 0;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
if(i < lim-2)
{
s[j] = c; // line still in boundaries
++j;
}
if(c == '\n')
{
s[j] = c;
++j;
++i;
}
s[j] = '\0';
return i;
}
// remove trailing blanks and tabs from character string s
int delete(char s[])
{
int i; i = 0;
while(s[i] != '\n') // find newline character
++i;
--i; // back off from '\n'
while(i > 0 && (s[i] == ' ' || s[i] == '\t'))
--i;
if(i >= 0) // is it a nonblank line?
{
++i;
s[i] = '\n'; // put newline character back
++i;
s[i] = '\0'; // terminate the string
}
return i;
}

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

#include<stdio.h>
#define MAXLINE 1000 // maximum input line size
int getline(char line[], int maxline);
void reverse(char s[]);
// reverse input lines, a line at a time
int main(void)
{
char line[MAXLINE]; // current input line while(getline(line, MAXLINE) > 0)
{
reverse(line);
printf("%s", line);
}
return 0;
}
// getline: read a line into s, return length
int getline(char s[], int lim)
{
int c, i, j; // i记录字符串长度,j记录被复制到字符串s中的字符的个数 j = 0;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
{
if(i < lim-2)
{
s[j] = c; // line still in boundaries
++j;
}
}
if(c == '\n')
{
s[j] = c;
++j;
++i;
}
s[j] = '\0';
return i;
}
// reverse: reverse string s
void reverse(char s[])
{
int i, j;
char temp; i = 0;
while(s[i] != '\0') // find the end of string s
++i;
--i; // back off from '\0'
if(s[i] == '\n')
--i; // leave newline in place
j = 0;
while(j < i) // beginning of new strings
{
temp = s[j];
s[j] = s[i]; // swap the characters
s[i] = temp;
--i;
++j;
}
}

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

#include<stdio.h>
#define TABINC 8 // tab increment size
// replace tabs with the proper number of blanks
int main(void)
{
int c, nb, pos; nb = 0; // number of blanks necessary
pos = 1; // position of character in line
while((c = getchar()) != EOF)
{
if(c == '\t') // tab character
{
nb = TABINC - (pos-1) % TABINC;
while(nb > 0)
{
putchar(' ');
++pos;
--nb;
}
}
else if(c == '\n') // newline character
{
putchar(c);
pos = 1;
}
else
{
putchar(c); // all other characters
++pos;
}
} return 0;
}

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. Git同账号多平台配置

    最近工作中使用到了Git,虽然以前学习过,但是已经忘的差不多了,遂将本次配置过程整理成笔记以备忘 生成公钥 ssh-keygen -t rsa -C "gana10007@163.com&q ...

  2. python中操作json

    1.导入json包 import json 2.打开json文档 fp = open(jsonpath) 3.读取json文件 data=json.load(fp) 4.获取json的值 data[' ...

  3. LintCode刷题笔记-- InterLeaving

    标签: 动态规划 解题思路 1. 这道题最重要的是,存在三个字符串,但是并不需要两个二维矩阵来进行解,因为可以使用i+j-1来代表s3的下标,这样就可以通过i和j来遍历s3了.因为对于任何一个合法的交 ...

  4. Vue.之.安装

    Vue.之.安装 第一步npm安装 首先:先从nodejs.org中下载nodejs   直到Finish完成安装. 打开控制命令行程序(CMD),检查是否正常 使用淘宝NPM 镜像 国内直接使用np ...

  5. 2019-5-13-WPF-从触摸消息转触摸事件

    title author date CreateTime categories WPF 从触摸消息转触摸事件 lindexi 2019-05-13 09:43:48 +0800 2019-05-12 ...

  6. 使用cnpm真的会有诡异的Bug

    前端网页居然会出现堆栈溢出,然后网页崩溃,退出的问题. 出现这个Bug的时候,我非常的怀疑我自己的一些操作能力,比如,git的操作. 毕竟我是本地代码然后拉取远程分支,还会暂存自己的代码,然后暂存区代 ...

  7. 【水滴石穿】ReactNativeDemo

    这个博主他的功底算是特别棒的了,能够把一些基础的例子,通过精巧的方式布局在一个小的demo里面 很值得我学习 放上博主的链接:https://github.com/jianiuqi/ReactNati ...

  8. docker出现如下错误:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

    在docker中配置deepo时出现了错误: 在出现这个错误之前,我是先用如下命令查看NVIDIA-docker是否安装成功. docker run --runtime=nvidia --rm nvi ...

  9. 【AtCoder Regular Contest 092】C.2D Plane 2N Points【匈牙利算法】

    C.2D Plane 2N Points 题意:给定N个红点二维坐标N个蓝点二维坐标,如果红点横纵坐标都比蓝点小,那么它们能够构成一组.问最多能构成多少组. 题解:把满足要求的红蓝点连线,然后就是匈牙 ...

  10. weixin 微信开放平台 微信公众平台

    官网地址入口 微信小程序 https://mp.weixin.qq.com/ appid and openid not match 1.appid :是公众号的ID. 2.openid:关注公众号生成 ...