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的更多相关文章

  1. C primer plus 第五版十二章习题

    看完C prime plus(第五版)第十二章,随带完成了后面的习题. 1.不使用全局变量,重写程序清单12.4的程序. 先贴出12.4的程序,方便对照: /* global.c --- 使用外部变量 ...

  2. 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---3

    字符串函数总结: 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1 void *memchr(const void *str, int c, size_t n)在参数  ...

  3. 《C prime plus (第五版)》 ---第11章 字符串和字符串函数

    11-1:字符串表示和字符串I/O 1.首先先通过一个整体的例子来初步了解建立,读入和输出字符串的几种方式. #include<stdio.h> #define MSG "你一定 ...

  4. 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---4

    字符串的例子: 1.字符串排序: 应用范围:准备花名册,建立索引以及很多情况下都会用刀字符串的排序.这个程序的主要工具就是strcmp(). 算法:读一个字符串数组,对它们进行排序并输出. #incl ...

  5. 《C prime plus (第五版)》 ---第12章 存储类.链接和内存管理

    12-1:存储类: 1.作用域: 代码块作用域,函数原型作用域和文件作用域. 2.链接:分为外部链接,内部链接和空链接.代码块作用域和函数原型作用域都是空连接,意味着是私有的.而文件作用域的变量可能是 ...

  6. 数据通讯与网络 第五版第24章 传输层协议-TCP协议部分要点

    上一博客记录了UDP协议的关键要点,这部分记录TCP协议的关键要点. 24.3 传输控制协议(TRANSMISSION CONTROL PROTOCOL) TCP(Transmission Contr ...

  7. 数据通讯与网络 第五版第24章 传输层协议-UDP协议部分要点

    24.1 介绍 本章节主要集中于传输层协议的解读,图24.1展示TCP.UDP.SCTP在TCP\IP协议栈的位置 24.1.1 服务(Service) 每个协议都提供不同的服务,所以应该合理正确的使 ...

  8. 《Python学习手册 第五版》 -第11章 赋值、表达式和打印

    上一章对Python的语句和语法已经进行了基本的说明,接下来就是每个章节的详细说明,本章的主要内容就是标题中涵盖的三点:赋值语句.表达式语句.打印语句 本章重点内容如下: 1.赋值语句 1)赋值语句的 ...

  9. 《Python学习手册 第五版》 -第13章 while循环和for循环

    上一章已经讲过if条件语句,这章重点是循环语句:while.for 本章的重点内容 1.while循环 1)一般形式 2)break.continue.pass和循环的else 2.for循环 1)一 ...

随机推荐

  1. C#数据之DataTable

    C#创建DataTable的几种方式 第一种方式:直接添加数据对象 DataTable table = new DataTable(); table.Columns.Add("strName ...

  2. Solidworks如何使用Toolbox

    Toolbox不仅仅是智能扣件.事实上,一般常见的轴承,螺栓,齿轮都有了,点击右侧的设计库即可展开Toolbox   配置完成后我只留下一个GB   比如我要选一个圆锥滚子轴承,从右边拖进来即可   ...

  3. apache支持php

    #tarzxvf php-5.2.9.tar.gz #cdphp-5.2.9 #./configure--prefix=/usr/local/php --with-apxs2=/usr/local/a ...

  4. HDOJ 2829 Lawrence

    四边形不等式优化DP Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. Shell脚本之:字符串

    字符串可以用单引号,也可以用双引号,也可以不用引号. 单引号 str='this is a string' 单引号字符串的限制: 1.单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 2 ...

  6. CentOS7.1 KVM虚拟化之经常使用管理虚拟机命令(3)

    一.查看虚拟机列表及状态 [root@kvm01 ~]# virsh list --all Id Name State ---------------------------------------- ...

  7. [ 转]C++ 虚函数表解析

    http://blog.csdn.net/haoel/article/details/1948051 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子 ...

  8. 关于安装oracle 11g client 出现安装先决条件检查全部失败

    本文转自:https://blog.csdn.net/iloli/article/details/45244159 今天我在安装Oracle11gClient时,全部显示成N/A,Oracle无法执行 ...

  9. python创建迅雷批量任务

    其实不是真的创建了批量任务,而是用python创建一个文本文件,每行一个要下载的链接,然后打开迅雷,复制文本文件的内容,迅雷监测到剪切板变化,弹出下载全部链接的对话框~~ 实际情况是这样的,因为用py ...

  10. erlang程序优化点的总结

    注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多调优启动参数,今后现在这个方面深挖一下. 1. 进程标志设置: 消息和binary内存:erla ...