《C prime plus (第五版)》 ---第11章 字符串和字符串函数---2
11-3:字符串 输出
三个标准的函数:puts() fputs() printf()
1.puts()函数:
#include<stdio.h>
#define DEF "I an a #define string ."
int main(){
char str1[] = "An array was initialized to me .";
const char *str2 = "A pointer was initialized to me."; puts("I'm an argument to puts().");
puts(DEF);
puts(str1);
puts(str2);
puts(&str1[]);
puts(str2+); return ;
}
在这个程序中,&str1[5]是数组str1的第6个元素。str2+4是i的那个内存单元,puts函数遇到空字符就会停下来。
2.fputs()函数,这个函数是面向文件的,与puts()函数的区别是:第二个参数说明要写的文件。同时,在输出的时候不会自动添加换行符。
3.printf()函数:不讲解。
4.自定义输入/输出函数:
void put1(const char *string){
while(*string)
putchar(*string++)
}
5.字符串函数:
1)strlen()函数:我们来看一个试图缩短字符串的函数。
#include<stdio.h>
#include<string.h>
void fit(char *,unsigned int);
int main(){
char mesg[] = "Hold on to your hats,hackers. ";
puts(mesg);
fit(mesg,);
puts(mesg);
puts("Let's look at some more of the string. ");
puts(mesg + );
return ;
}
void fit(char *string,unsigned int size){
if(strlen(string) > size){
*(string + size) = '\0';
}
}
2)strcat()函数:接受两个字符串参数。第二个字符串会添加到第一个字符串后边,然后返回第一个字符串。同时第一个字符串改变,第二个字符串不变。
3)strncat()函数:接受三个参数,第三个参数是数字,是函数最多接受的字符数或者遇到空字符为止。
#include<stdio.h>
#include<string.h>
#define SIZE 30
#define BUGSIZE 13
int main(){
char flower[SIZE];
char addon[] = "s smell like old shoes.";
char bug[BUGSIZE];
int available; puts("What is yout favorite flowers?");
gets(flower);
if((strlen(addon) + strlen(flower) + )<= SIZE)
strcat(flower,addon);
puts(flower);
puts("What is your favorite bug?");
gets(bug);
available = BUGSIZE - strlen(bug) - ;
strncat(bug,addon,available);
puts(bug); return ;
}
4)strcmp()函数:这个函数用来比较两个数组中的字符串的内容。
这个程序来判断程序是否应该停止读取输入:
#include<stdio.h>
#include<string.h>
#define SIZE 81
#define LIM 100
#define STOP "quit"
int main(){
char input[LIM][SIZE];
int ct = ; printf("Enter up to %d lines(type quit to quit):\n",LIM);
while(ct < LIM && gets(input[ct]) != NULL && strcmp(input[ct],STOP) != ){
ct++;
}
printf("%d strings entered\n",ct); return ;
}
5)strncmp()函数:这个函数有第三个参数,用来限定比较字符串的个数。
#include<stdio.h>
#include<string.h>
#define LISTSIZE 5
int main(){
char *list[LISTSIZE] = {
"astronomy",
"astounding",
"astronphysics",
"ostracize",
"asterusm"
};
int count = ;
int i ;
for(i = ; i < LISTSIZE; i++){
if(strncmp(list[i],"astro",) == ){
printf("Found:%s\n",list[i]);
count++;
}
}
printf("The list contained %d words beginning""with astro.\n",count); return ;
}
6)strcpy()函数和strncpy()函数:
#include<stdio.h>
#include<string.h>
#define SIZE 40
#define LIM 5
int main(){
char qwords[LIM][SIZE];
char temp[SIZE];
int i = ; printf("Enter %d words beginning with q\n",LIM);
while(i < LIM && gets(temp)){
if(temp[] != 'q'){
printf("%s doesn't begin with q!\n",temp);
}
else{
strcpy(qwords[i],temp);
i++;
}
}
puts("Here are the words accepted:");
for(i = ; i < LIM ; i++){
puts(qwords[i]);
} return ;
}
strcpy()函数接受两个字符串指针参数,指向最初字符串的第二个指针可以是已声明的指针,数组名或字符串常量。声明一个数组将为数据分配存储空间,而声明一个指针只为一个地址分配存储空间。
strcpy()函数的高级属性:该函数返回第一个参数的值,一个字符的地址,所以这样可以复制数组的一部分。
#include<stdio.h>
#include<string.h>
#define WORDS "beast"
#define SIZE 40 int main(){
char *orig = WORDS ;
char copy[SIZE] = "Be the best that you can be.";
char *ps ; puts(orig);
puts(copy);
ps = strcpy(copy+,orig);
puts(copy);
puts(ps);
}
strncpy()函数:这个函数接受第三个参数指明复制的大小。
#include<stdio.h>
#include<string.h>
#define SIZE 40
#define TARGSIZE 7
#define LIM 5
int main(){
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i = ; printf("Enter %d words beginning with q :\n",LIM);
while( i < LIM && gets(temp)){
if(temp[] != 'q'){
printf("%s doesn't begin with q!\n",temp);
}
else{
strncpy(qwords[i],temp,TARGSIZE - );
qwords[i][TARGSIZE - ] ='\0';
i++;
}
}
puts("Here are the words accepted: ");
for(i = ; i < LIM ; i ++){
puts(qwords[i]);
} return ;
}
7)sprintf()函数:提供了把几个元素合成一个字符串。第一个参数是目标字符串的地址,其余参数和printf()函数一样。
#include<stdio.h>
#define MAX 20 int main(){
char first[MAX];
char last[MAX];
char formal[*MAX + ];
double prize ; puts("Enter your first name:");
gets(first);
puts("Enter your last name");
gets(last);
puts("Enter your prize money");
scanf("%lf",&prize);
sprintf(formal,"%s,%-19s:RMB%6.2f\n",last,first,prize);
puts(formal); return ;
}
《C prime plus (第五版)》 ---第11章 字符串和字符串函数---2的更多相关文章
- C primer plus 第五版十二章习题
看完C prime plus(第五版)第十二章,随带完成了后面的习题. 1.不使用全局变量,重写程序清单12.4的程序. 先贴出12.4的程序,方便对照: /* global.c --- 使用外部变量 ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---3
字符串函数总结: 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1 void *memchr(const void *str, int c, size_t n)在参数 ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数
11-1:字符串表示和字符串I/O 1.首先先通过一个整体的例子来初步了解建立,读入和输出字符串的几种方式. #include<stdio.h> #define MSG "你一定 ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4
字符串的例子: 1.字符串排序: 应用范围:准备花名册,建立索引以及很多情况下都会用刀字符串的排序.这个程序的主要工具就是strcmp(). 算法:读一个字符串数组,对它们进行排序并输出. #incl ...
- 《C prime plus (第五版)》 ---第12章 存储类.链接和内存管理
12-1:存储类: 1.作用域: 代码块作用域,函数原型作用域和文件作用域. 2.链接:分为外部链接,内部链接和空链接.代码块作用域和函数原型作用域都是空连接,意味着是私有的.而文件作用域的变量可能是 ...
- 数据通讯与网络 第五版第24章 传输层协议-TCP协议部分要点
上一博客记录了UDP协议的关键要点,这部分记录TCP协议的关键要点. 24.3 传输控制协议(TRANSMISSION CONTROL PROTOCOL) TCP(Transmission Contr ...
- 数据通讯与网络 第五版第24章 传输层协议-UDP协议部分要点
24.1 介绍 本章节主要集中于传输层协议的解读,图24.1展示TCP.UDP.SCTP在TCP\IP协议栈的位置 24.1.1 服务(Service) 每个协议都提供不同的服务,所以应该合理正确的使 ...
- 《Python学习手册 第五版》 -第11章 赋值、表达式和打印
上一章对Python的语句和语法已经进行了基本的说明,接下来就是每个章节的详细说明,本章的主要内容就是标题中涵盖的三点:赋值语句.表达式语句.打印语句 本章重点内容如下: 1.赋值语句 1)赋值语句的 ...
- 《Python学习手册 第五版》 -第13章 while循环和for循环
上一章已经讲过if条件语句,这章重点是循环语句:while.for 本章的重点内容 1.while循环 1)一般形式 2)break.continue.pass和循环的else 2.for循环 1)一 ...
随机推荐
- Flume NetCat Demo
准备工作: 1.apache官网下载flume 2.解压flume 3.修改flume-env.sh,配置JAVA_HOME NetCat采集Demo: 1.在conf中创建netcat-logger ...
- linux中read用法
read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...
- JAVA Eclipse开发Android如何设置滚动条最大值最小值
最小值默认为0,你最好在实现逻辑中修改 最大值为max 初始值为progress <SeekBar android:id="@+id/seekBarSpeedMov ...
- Material Design Get Started
使用Material Design设计应用: Take a look at the material design specification. Apply the material theme to ...
- Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...
- Android-Animations介绍
一.Animations介绍 Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转.缩放.淡入淡出等,这些效果可以应用在绝大 ...
- POJ3009 Curling 2.0(DFS)
迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...
- Portal实现原理
https://blog.csdn.net/sukyle/article/details/6456930
- 使用T4模板为EF框架添加数据库实体注释
网上有一个解决方法如下: http://www.cnblogs.com/stone_w/archive/2012/10/25/2738345.html 不过我试了下没解决太麻烦了 而且一旦EF要重新生 ...
- mac下spark单机环境配置笔记
1.安装scala 从http://www.scala-lang.org下载scala-2.11.7.tgz并解压缩 将解压缩的文件夹用mv指令移动到/usr/local/share mv [scal ...