C语言 参数传递
来源:互联网 发布:新手学电脑打字软件 编辑:程序博客网 时间:2024/05/01 07:24
编写一个程序,删除每一个输入行末尾的空格及制表符,并删除完全是空格的行。
这边之前已经有了获取一个输入行的函数:
int getline(char *line, int maxline){ char ch; int len; len = 0; while((ch = getchar()) != '\n' && ch != EOF && len < maxline) { line[len++] = ch; } line[len]= '\0'; return len;}
下面是针对于一行语句进行trim操作,我们函数命名del0char:
int del0char(char *line){ int len = strlen(line); //find the first ' ' or '\t' char ch = line[--len]; //printf("len = %d~", len); while( (ch == ' ' || ch == '\t') && len >= 0) { ch = line[--len]; } //if the last char of line is blank,then change something if(line[len+1] == ' ') line[len+1] = '\0';}
然后在主方法中做测试:
int main(int argc, char *argv[]){ char lines[MAXLEN][MAXLINE]; char line[MAXLINE]; int len ; int j; int i = 0; while((len = getline(line, MAXLINE)) != 0) { printf("before: %s~\n", line); del0char(line); strcpy(lines[i++], line); } //press twice 'enter' exit the program for(j = 0; j< i; j ++) { printf("finally: %s~\n",lines[j]); }}
其中
#define MAXLEN 100#define MAXLINE 100
测试结果:
字符串的倒序操作:
//reverse the stringvoid reverse(char *s){ int len; int i,j; len = strlen(s); i = 0; j = len -1; for(; i < j; i++,j--) { swap(s, i, j); }}int main(int argc, char *argv[]){ char line[MAXLEN]; int len ; while((len = getline(line, MAXLINE)) > 0) { reverse(line); printf("%s\n",line); } return 0;}
test result:
当然,网上有很多的这样的类似字符串倒序算法。
//decode the '\t' to blankvoid detab(char *line, char *newLine, int len){ int i ,j ; for(i = 0,j=0; i<len; i++) { if(line[i] == '\t') { //calculate the number of blank, and print them into memory int k = 8-i%8; while(k>0) { newLine[j++] = ' '; k--; } }else { newLine[j++] = line[i]; } } newLine[j] = '\0';}
这边是这样子的,把制表符‘\t’置换成一定数量的空格。上面中我们可以把制表符的长度8置换成常量。现在我们反过来编写将一定数量的空格改写成制表符,尽管在显示上看起来是没有什么样的任何变化的,实际上制表符的所用内存就会是少了很多。
0 0
- c语言参数传递
- C语言参数传递
- C语言参数传递
- C语言参数传递
- C语言参数传递
- C语言参数传递
- C语言 参数传递
- C语言参数传递
- c语言传递参数
- C语言的参数传递
- C语言的参数传递
- C语言的参数传递
- C语言函数参数传递
- c语言参数传递问题
- c语言的参数传递
- c语言函数参数传递
- c语言参数传递方式
- C语言中的参数传递
- Ionic2可以有两种方式来存储数据,Local Storage和SQLite
- 初识WebDriver
- Project build error: Invalid packaging for parent POM cn.itcast.maven:Parent:0.0.1-SNAPSHOT (D:\Prog
- wsprintf用法
- 调用新浪接口查询IP地址
- C语言 参数传递
- [总结]FFMPEG视音频编解码零基础学习方法
- iOS中GCD的使用小结
- 欢迎使用CSDN-markdown编辑器
- 第九周项目1——猴子选大王(数组版)
- 2222
- 【刷题】移动盒子问题
- tomcat启动报错 ERROR o.a.catalina.session.StandardManager 182 - Exception loading sessions from persiste
- 只怪自己读书少