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. python面向对象(二)

    初始化方法 当使用 类名() 创建对象时,会 自动 执行以下操作: 为对象在内存中 分配空间 —— 创建对象 为对象的属性 设置初始值 —— 初始化方法(init) 这个 初始化方法 就是 __ini ...

  2. Oracle中Function学习2

    上一节说函数 这回聊一下 存储过程:有了上节知识点,对这个也许会一种小感觉.呵呵 过程(存储过程):与过程相比, 存储过程是存在数据库中的一个对象 我主要通过例子来了解 不做多解释 相信你也能明白: ...

  3. console.js还有浏览器不支持?

    今天看到项目中引入了一个插件,我超级惊讶 为什么引入console.js啊? 这个是插件的源码:https://github.com/yanhaijing/console.js 我搜到源作者对这个插件 ...

  4. 在MaxCompute中利用bitmap进行数据处理

    很多数据开发者使用bitmap技术对用户数据进行编码和压缩,然后利用bitmap的与/或/非的极速处理速度,实现类似用户画像标签的人群筛选.运营分析的7日活跃等分析.本文给出了一个使用MaxCompu ...

  5. 转: PHP的"::"、"->"和"=>"的区别

    来自:http://blog.csdn.net/wgw335363240/article/details/20453379 1."::" 运算法 该运算符是调用一个类中的静态成员的 ...

  6. 关于502 bad gateway报错的解决办法

  7. 订阅 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件

    如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件 标签: idea git 插件 分类: Git 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...

  8. Mysql常用的三种数据库引擎比较

    ISAM:ISAM是一个定义明确且历经时间考验的数据表格管理方法,它在设计之时就考虑到数据库被查询的次数要远大于更新的次数.因此,ISAM执行读取操作的速度很快,而且不占用大量的内存和存储资源.ISA ...

  9. Intellij IDEA设置忽略部分类编译错误

    有些时候我们的项目中有些错误,但这些错误并不影响项目的整体运行(或许是没有使用到),默认情况下idea是无法通过编译的,因此也就无法部署运行,要达到正确运行项目的目的需要作一些设置才行. 设置Inte ...

  10. TCPThree_C杯 Day2

    T1 我已经被拉格朗日插值蒙蔽了双眼,变得智障无比. 第一反应就是拉格朗日插值,然后就先放下了它. 模数那么小,指数那么大,这是一套noip模拟题,拉格朗日,你脑袋秀逗了? 无脑暴力20分贼开心. 正 ...