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. nx sdk

    最近在做ns 任天堂略坑.. 他想做成一键安装,可总是有些问题,这样再去找就很麻烦了.都是包在里面的,要看很多文档 第一个问题是 NintendoSDK Configuration Manager 这 ...

  2. Incorrect column count: expected 1, actual 2

    List<Long> idList = queryForList("ass.pageQuery_sgIds", paramMap, Long.class); 报错:In ...

  3. JAVA Eclipse创建Android程序界面不显示怎么办

    一般是由于你创建的Android应用程序版本太高导致的,请设置4或以下版本,对于已有的项目,可以在属性-Android中修改目标生成的版本号                               ...

  4. 微信小程序 - 下载图片并且显示进度

    lastUpDate: 2018-8-12 请把请求地址加入到downLoadFile 提示:首先得要在公众号设置对应的downLoadFile地址. downImg : 下载图片 wxml js d ...

  5. GUID概念

     GUID概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是 ...

  6. Solr局部或指定字段更新之set用法

    solr wiki文档也有        http://yonik.com/solr/atomic-updates/         java code   public static void up ...

  7. FZU2125:简单的等式

    Problem Description 如今有一个等式例如以下:x^2+s(x,m)x-n=0. 当中s(x,m)表示把x写成m进制时,每一个位数相加的和.如今,在给定n,m的情况下,求出满足等式的最 ...

  8. OpenSUSE 13.1上安装StrongSwan

    结果: 1)iOS 7.1设备能够拨IPSec VPN到StrongSwan电脑上面来 - Connect to VPN 2)iOS 设备浏览器能够訪问StrongSwan VPN所在的内网地址服务器 ...

  9. AWR元数据的迁移或导入到其它数据库

    我们能够将AWR元数据迁移(导入)到其它数据库.低版本号的导入到高版本号,再用高版本号的数据库生成AWR报告,也能使用一些新特性,如 SQL ordered by Physical Reads (Un ...

  10. C# C/S程序使用HTML文件作为打印模板

    C#   C/S程序使用HTML文件作为打印模板 在网上找了一堆的资料,整理到郁闷呀,慢慢试慢慢改.哎,最终成功了,哈,菜鸟伤不起呀 public partial class Print : Form ...