C Primer Plus 第十一章程序清单……2015.5.7

C Primer Plus

          第五版

第十一章 字符串和字符串函数
11.1
#include<stdio.h>
#define MSG "You must have many talents.Tell me some."
#define LIM 5
#define LINELEN 81
int main(void)
{
char name [LINELEN];
char talents[LINELEN];
int i;
const char m1[40]="Limit yourself to one line's worth.";
const char m2[]="If you can't think of anything,fake it.";
const char *m3="\nEnough about me-what's your name?";
const char *mytal[LIM]={"ADDing","MUltiplying","STashing","Following","Under"};
printf("Hi");
for(i=0;i<LIM;i++)
 puts(mytal[i]);
puts(m3);
gets(name);
printf("%s\n%s\n",m1,m2);
gets(talents);
puts(talents);
printf("%s",name);
return 0;

程序清单11.2
#include<stdio.h>
int main(void)
{
printf("%s,%p,%c\n","we","are",*"space farers");
return 0;
}


程序清单11.3
#include<stdio.h>
int main(void)
{
char *mesg="Don't be a fool! ";
char *copy;
copy=mesg;
printf("%s\n",copy);
printf("mesg=%s;&mesg=%p;value=%p\n",mesg,&mesg,mesg);
printf("copy=%s;&copy=%p;value=%p\n",copy,&copy,copy);
return 0;
}
程序清单11.4
#include<stdio.h>
#define MAX 81
int main(void)
{
char name[MAX];
printf("hi,what's your name?\n");
gets(name);
puts(name);
return 0;
}
程序清单11.5
#include<stdio.h>
#define MAX 81
int main(void)
{
char name[MAX];
char*ptr;
printf("hi,what's your name?\n");
ptr=gets(name);
puts(ptr);
puts(name);
return 0;
}
程序清单11.6
#include<stdio.h>
#define MAX 81
int main(void)
{
char name[MAX];
char*ptr;
printf("hi,what's your name?\n");
ptr=fgets(name,MAX,stdin);
printf("%s%s",ptr,name);
return 0;
}


程序清单11.7
#include<stdio.h>
 
int main(void)
{
char name[11],name1[11];
int count;
printf("hi,enter two name?\n");
count=scanf("%5s %10s",name,name1);

printf("I read %d name %s and %s",count,name,name1);
return 0;
}


程序清单11.8
#include<stdio.h>
#define DEF "I am a #define string."
int main(void)
{
char str1[80]="An array was sdfsd sdvf";
const char *str2="A pointer was ajcnja.";
puts("I am an akbnck");
puts(DEF);
puts(&str1[5]);
puts(str1);
puts(str2);
puts(str2+4);
return 0;
}






程序清单11.10
#include<stdio.h>
void put1(const char*string);
int main(void)
{
const char * ptr="svbskvnskvnjc";
put1(ptr);
return 0;
}
void put1(const char*string)
{
while(*string!='\0')
putchar(*string++);
}




程序清单11.11
#include<stdio.h>
int put2(const char* string);
int main(void)
{
char ptr[10];
gets(ptr);
  printf("%d",put2(ptr));
return 0;

int put2(const char* string)
{
int count=0;
while(*string)
{
putchar(*string++);
count++;
}
putchar('\n');
return (count);





程序清单11.12
#include<stdio.h>
#include<string.h>
void fit(char*,unsigned int);
int main(void)
{
char mesg[]="Hold on to your hats,hackers,";
puts(mesg);
fit(mesg,7);
puts(mesg);
puts("Let's look at some more of the string.");
puts(mesg+8);
return 0;
}
void fit(char *string,unsigned int size)
{
if(strlen(string)>size)
    *(string+size)='\0';
}
程序清单11.13
#include<stdio.h>
#include<string.h>
#define SIZE 80
int main(void)
{
char flower[SIZE];
char addon[]="s smell like old shoes.";
puts("what is your favorite flowe?r");
gets(flower);
strcat(flower,addon);
puts(flower);
puts(addon);
return 0;
}




程序清单11.15
#include<stdio.h>
#include<string.h>
#define SIZE 30
#define BUGSIZE 13
int main(void)
{
char flower[SIZE];
char addon []="s smell like old shoes.";
char bug[BUGSIZE];
int available;
puts("WHat is your favorite flower?");
gets(flower);
if((strlen(addon)+strlen(flower)+1)<SIZE)
     strcat(flower,addon);
puts(flower);
puts("what is your favorite bug?");
gets(bug);
available=BUGSIZE-strlen(bug)-1;
strncat(bug,addon,available);
puts(bug);
return 0;
}
程序清单11.16
#include<stdio.h>
#define ANSWER "Grant"
int main(void)
{
char try[40];
puts("who is buried in Grant's tomb?");
gets(try);
while(try!=ANSWER)
{
puts("No,that`s wrong.Try again.");
gets(try);
}
puts("that's right!");
return 0;
}
程序清单11.17
#include<stdio.h>
#include<string.h>
#define ANSWER "Grant"
int main(void)
{
char try[40];
puts("who is buried in Grant's tomb?");
gets(try);
while(strcmp(try,ANSWER)!=0)
{
puts("No,that`s wrong.Try again.");
gets(try);
}
puts("that's right!");
return 0;
}
程序清单11.18
#include<stdio.h>
#include<string.h>
int main(void)
{
printf("\"A\",\"A\" is %d\n",strcmp("A","A"));
printf("\"A\",\"B\" is %d\n",strcmp("A","B"));
 
printf("\"C\",\"B\"  is %d\n",strcmp("C","A"));
printf("\"A\",\"a\" is %d\n",strcmp("A","a"));
return 0;
}
程序清单11.19
#include<stdio.h>
#include<string.h>
#define SIZE 81
#define LIM 100
#define STOP "quit"
int main(void)
{
char input[LIM][SIZE];
int ct=0;
printf("Enter up to %d lines\n",LIM);
while(ct<LIM&&gets(input[ct])!=NULL&&strcmp(input[ct],STOP)!=0)
{
ct++;

}
printf("%d string entered\n",ct);
return 0;
}
程序清单11.20
#include<stdio.h>
#include<string.h>
#define LISTSIZE 5
int main(void)
{
char*list[LISTSIZE]={"Astronomy","astounding","astrophysics","ostracize","astroism"};
int count=0;
int i;
for(i=0;i<LISTSIZE;i++)
   if(strncmp(list[i],"astro",5)==0)
   {
    printf("Found:%s\n",list[i]);
    count++;
   }
   printf("The list contained %d words beginning",count);
return 0;
}


程序清单11.21
#include<stdio.h>
#include<string.h>
#define SIZE 40
#define LIM 5
int main(void)
{
char qwords[LIM][SIZE];
char temp[SIZE];
int i=0;
printf("Enter %d  words beginning with \n",LIM);
while(i<LIM&&gets(temp))
{
if(temp[0]!='q')
  printf("%s doesnt begin whit q!\n",temp);
else
{
strcpy(qwords[i],temp);
i++;
}
}
puts("Here are the words accepted: ");
for(i=0;i<LIM;i++)
 puts(qwords[i]);
 return 0;
}
程序清单11.22


#include<stdio.h>
#include<string.h>
#define WORDS "beast"
#define SIZE 40
int main(void)
{
char *orig=WORDS;
char copy[SIZE]="Be the best that you can be.";
char *ps;
puts(orig);
puts(copy);
ps=strcpy(copy+7,orig);
puts(copy);
puts(ps);
return 0;
}
程序清单11.23
#include<stdio.h>
#include<string.h>
#define TARGSIZE 7
#define LIM 5
#define SIZE 40
int main(void)
{
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i=0;
printf("Enter %d words beginning with\n",LIM);
while(i<LIM&&gets(temp))
{
if(temp[0]!='q')
  printf("%s doesnt begin whit q!\n",temp);
else
{
strncpy(qwords[i],temp,TARGSIZE-1);
qwords[i][TARGSIZE-1]='\0';//确保真正复制后成为一个字符串 
i++;
}
}
puts("Here are the words accepted: ");
for(i=0;i<LIM;i++)
 puts(qwords[i]);
 return 0;


}
程序清单11.24
#include<stdio.h>
#define MAX 20
int main(void)
{
char first[MAX];
char last[MAX];
char formal[50];
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 %lf\n",last,first,prize);
puts(formal);
return 0;

程序清单11.25
#include<stdio.h>
#include<string.h>
#define SIZE 81
#define LIM 20
#define HALT " "
void start(char *string[],int num);
int main(void)
{
char input[LIM][SIZE];
char *ptstr[LIM];
int ct=0;
int k;
printf("input up to %d lines\n",LIM);
while(ct<LIM&&gets(input[ct])!=NULL&&input[ct][0]!='\0')
{
ptstr[ct]=input[ct];
ct++;
}
start(ptstr,ct);
// puts("\nhbvsjvbsjkbvhsjk\n");
for(k=0;k<ct;k++)
 puts(ptstr[k]);
 return 0;
}
void start(char *string[],int num)
{
char *temp;
int i,j;
for(i=0;i<num-1;i++)
  for(j=i+1;j<num;j++)
     if(strcmp(string[i],string[j])>0)
   {
    temp=string[i];
     string[i]=string[j];
     string[j]=temp;
   }  
}




程序清单11.26
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define LIMIT 80
void ToUpper(char *);
int PunctCount(const char *);
int main(void)
{
char line[LIMIT];
gets(line);
ToUpper(line);
puts(line);
printf("That line has %d jsdk\n",PunctCount(line));
return 0;
}
void ToUpper (char*str)
{
while(*str)
{
*str=toupper(*str);
str++;
}
}
int PunctCount(const char *str)
{
int ct=0;
while(*str)
{
if(ispunct(*str))
ct++;
str++;
}
return ct;
}


程序清单11.27
#include<stdio.h>
int main(int argc,char*argv[])
{
int count;
printf("%d",argc-1);
for(count=1;count<argc;count++)
   printf("%d:  %s\n",count,argv[count]);
printf("\n");
return 0;
}


程序清单11.28
#include<stdio.h>
#include<stdlib.h>
 
int main(int argc,char*argv[])
{
int i,times;
printf("%d",argc );
if(argc<2||(times=atoi(argv[1]))<1)
   printf("  %s\n", argv[0]);
else
   for(i=0;i<times;i++)
      puts("Hello");
 
return 0;
}
程序清单11.29
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char number[30];
char*end;
long value;
puts("Enter");
while(gets(number)&&number[0]!='\0')
{
value=strtol(number,&end,10);
printf("%ld %s %d\n",value,end,*end);
value=strtol(number,&end,16);
printf("%ld %s %d\n",value,end,*end);
puts("Next number:\n");
}
puts("BYe");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值