《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)一 ...
随机推荐
- VirtualBox导入XXXX.vdi时报错
virtualbox导入vdi文件时出现以下的问题: 解决方法: windows+R,输入cmd,进入virtualbox的安装文件夹(或者在硬盘中直接进入virtualbox的安装文件夹.在任务栏里 ...
- DevExpress控件之TreeList
基于v18.1 使用AppendNode方法手动赋值时,首先要添加treeListColumn 默认样式 修改后的样式 1 ...
- 香蕉派(or 皮?)上手初体验 -- 外观鉴赏,安装,配置&总结
一.前言及简单介绍 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ2Vyem9uZQ==/font/5a6L5L2T/fontsize/400/f ...
- vue2.0 watch 详解
vue官网解释: 一个对象,键是需要观察的表达式,值是对应回调函数.值也可以是方法名,或者包含选项的对象.Vue 实例将会在实例化时调用 $watch(),遍历 watch 对象的每一个属性. 也就是 ...
- 算法-search
O(big o) 是上限,是我们关注的算法的时间复杂度.数据量大,数据量涨一千倍,lgn的算法就是 耗费的时间就是10倍,o(n)就是一千倍,o(n2)就是一百万倍的差距 例一:Sequential ...
- 点击出现黑色背景的解决:-webkit-tap-highlight-color:rgba(0,0,0,0)
在手机上(iphone)点击按钮的时候,屏幕总会闪动一下,这让页面看起来很不友好也不流畅.解决方案加了一句css就解决了: -webkit-tap-highlight-color:rgba(0,0,0 ...
- Command &Prompt Here
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\Command Prompt Here]@=" ...
- Anaconda2
Anaconda 是一个打包的python,一次把好多需要的包都安装好了.对于Python2.7把PyQt5都弄好了,不需要自己来编译! 看看这个 http://conda.pydata.org/do ...
- 世界更清晰,搜狐新闻客户端集成HUAWEI HiAI 亮相荣耀Play发布会!
6月6日,搭载有“很吓人”技术的荣耀Play正式发布,来自各个领域的大咖纷纷为新机搭载的惊艳技术站台打call,其中,搜狐公司董事局主席兼首席执行官张朝阳揭秘:华为和搜狐新闻客户端在硬件AI方面做 ...
- 字符串转化成十六进制输出StrToHex(Delphi版、C#版)
//注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版字符串事先须经过AnsiToUtf8()转码才能跟C#版得到的十六进制字符串显示结果一 ...