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. webpack4进阶配置

    移动端CSS px自动转换成rem 需要两步来实现: px2rem-loader 在构建阶段将px转换成rem lib-flexible 页面渲染时动态计算根元素的font-size值(手机淘宝开源库 ...

  2. IE9没有内置鼠标手势,还要自己写

    写了个IE插件,然后获取鼠标,信息, 模拟了鼠标手势,在虚拟机里面测试,完全好使,但是现在又不敢在Win7上用了. 愁死了... 为了实现一个鼠标手势. 写的那破玩意,竟然50多K.....太大了.. ...

  3. Vue. 之 Element table 高度自适应

    Vue. 之 Element table 高度自适应 使用vue创建table后,其高度自适应浏览器高度. 在创建的 el-table 中添加:height属性,其值为一个变量(tableHeight ...

  4. SQL Server 存储过程详解

    转自:https://blog.csdn.net/younghaiqing/article/details/62884658 一. 什么是存储过程 系统存储过程是系统创建的存储过程,目的在于能够方便的 ...

  5. WPF数据绑定详解

    元素绑定 数据绑定最简单的形式是,源对象是WPF元素而且源属性是依赖属性.依赖项属性具有内置的更改通知支持,当在源对象中改变依赖项属性的值时,会立即更新目标对相中的绑定属性. <!--Xaml程 ...

  6. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. OSGi Capabilities

    OSGi bundle的Capability就是这个bundle所具有的能力. 就像淘宝上的每个店铺一样,它会说明自己都卖哪些东西,也就是Provide-Capability 我们这些剁手党就会根据自 ...

  8. JavaScript-- 函数既是函数又是对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Codeforces 414A

    题目链接 首先考虑无解的情况: n / 2 > k 或者 n==1 且 k != 0 (因为两个数的最大公约数最小为1) 然后因为有 n / 2 组(把 a[i] 和 a[i+1] 看成一组), ...

  10. word Stock Market Indices

    Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America Internation ...