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 ;
}