Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time.

#include <stdio.h>

#define MAX_LINE 1024

void discardnewline(char s[])
{
int i;
for(i = ; s[i] != '\0'; i++)
{
if(s[i] == '\n')
s[i] = '\0';
}
} int reverse(char s[])
{
char ch;
int i, j; for(j = ; s[j] != '\0'; j++)
{
} --j; for(i = ; i < j; i++)
{
ch = s[i];
s[i] = s[j];
s[j] = ch;
--j;
} return ;
} int getline(char s[], int lim)
{
int c, i; for(i = ; i < lim - && (c = getchar()) != EOF && c != '\n'; ++i)
{
s[i] = c;
} if(c == '\n')
{
s[i++] = c;
} s[i] = '\0'; return i; } int main(void)
{
char line[MAX_LINE]; while(getline(line, sizeof line) > )
{
discardnewline(line);
reverse(line);
printf("%s\n", line);
}
return ;
}

c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。的更多相关文章

  1. c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出

    Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...

  2. c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()

    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...

  3. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格

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

  4. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  5. c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

      fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...

  6. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  7. c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

    Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...

  8. c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件

    How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...

  9. 网易云课堂_程序设计入门-C语言_第五周:函数_2完数

    2 完数(5分) 题目内容: 一个正整数的因子是所有可以整除它的正整数.而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数.例如6=1+2+3(6的因子是1,2,3). 现在,你要写一个程序, ...

随机推荐

  1. Android NDK 环境搭建 + 测试例程

    懒得废话一大堆概念,关于ADT.NDK的概念要是你不懂,怎么会搜到这里来?所以你只需要根据下面的步骤来,就可以完成NDK环境搭建了. 步骤:(假设你未安装任何相关开发工具,如果已经安装了,就可以跳过) ...

  2. 此一生 一个纯js的ajax

    /** * 得到ajax对象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp ...

  3. 踩过的坑系列之InputStream.read(byte[])方法

    项目之前都是好好的,最近现场那边出现一个问题,报错不是合法的json字符串,这个json字符串是通过http请求访问获得的. 通过直接在浏览器上直接访问http这个请求,发现返回的json也是完全正确 ...

  4. POJ2255二叉树

    题目大意就是给出你一个二叉树的前序和中序,要你求后序. 思路:二叉树的排序就是根据根节点的位置来定义的.所以找到二叉树的根节点是最重要的,二叉树的左子树和右子树也可以看成是二叉树,以此递归: #inc ...

  5. 51nod贪心算法入门-----活动安排问题2

    题目大意就是给几个活动,问要几个教室能够弄完. 这个题目的想法就是把活动的开始——结束的时间看做是数轴上的一段线段,教室的个数就是在某点的时间厚度,求最大的时间厚度就是所需要的教室个数. #inclu ...

  6. codeforces 633D - Fibonacci-ish 离散化 + 二分查询

    Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...

  7. 深度(Depth)概念

    强化对深度的理解 在老版本的NGUI中,UI的显示层次关系是依靠z轴进行的.在新版本的NGUI中,所有UI的z轴都被统一,然后用深度来决定和管理显示的层次关系.关于深度,要记住一下关键点: 1.每一个 ...

  8. css 垂直同步的几种方式

    第一种: 利用 display:table 和 display:table-cell 的方式 这种方式就好像将table布局搬过来一样,相信大家对table的td还是有印象的,它的内容是可以设置垂直居 ...

  9. mysql查询结果中文显示成了问号

    在mysql的配置文件my.ini中的[mysqld]项中加这两句 character-set-server = utf8 collation-server = utf8_general_ci 在任务 ...

  10. <django中render_to_response的可选参数和使用方法>

    在django官方文档中有比较详细的介绍,在此我按照自己的理解适当的阐述一下: return render_to_response(①'my_template.html', ②my_data_dict ...