zoukankan      html  css  js  c++  java
  • c primer plus 习题答案(4)

    p319.3

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<ctype.h>
     4 #include<string.h>
     5 void deliver(char *a1, char *a2, int n);
     6 
     7 int main(void)
     8 {
     9     int n;
    10     char str1[81], *ptr, ar[81];
    11     ptr=gets(str1);
    12     n=strlen(ptr);
    13     deliver(ar, ptr, n);
    14     puts(ar);
    15 
    16     system("pause");
    17     return 0;
    18 }
    19 
    20 void deliver(char *a1, char *a2, int n)
    21 {
    22     int i;
    23     for(i=0; i<n; i++){
    24         *(a1+i)=*(a2+i);
    25         if(isspace(*(a1+i)))
    26             break;
    27     }
    28     if(i==n)
    29         *(a1+i)='';
    30     else
    31         *(a1+i+1)='';
    32 }

    p320.5

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 int is_within(char *ar, char ch);
     4 
     5 int main(void)
     6 {
     7     char ar[81], ch;
     8     do
     9     {
    10     puts("input a range string:");
    11     gets(ar);
    12     puts("input match char(enter q to quit):");
    13     ch=getchar();
    14     getchar();
    15     if(is_within(ar, ch)) puts("match");
    16     else puts("can't find");
    17 
    18     }while(ch!='q');
    19 
    20     system("pause");
    21     return 0;
    22 }
    23 
    24 int is_within(char *ar, char ch)
    25 {
    26     while(*ar!='')
    27         if(*ar==ch)
    28             return 1;
    29         else
    30             ar++;
    31     return 0;
    32 }

    p320.6

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 char *strncpy(char *s1, char *s2, int n);
     5 
     6 int main(void)
     7 {
     8     char s1[81], s2[81];
     9     int n;
    10     puts("please input a range string for s2:");
    11     gets(s2);
    12     puts("please input a range string for s1:");
    13     gets(s1);
    14     puts("please input a number of copies:");
    15     scanf("%d", &n);
    16     getchar();
    17 
    18     strncpy(s1, s2, n);
    19     puts(s1);
    20 
    21     system("pause");
    22     return 0;
    23 }
    24 
    25 char *strncpy(char *s1, char *s2, int n)
    26 {
    27     int i;
    28 
    29     if(n<=strlen(s2)){
    30         for(i=0; i<n; i++)
    31             *(s1+i)=*(s2+i);
    32     *(s1+i)='';
    33     }
    34     
    35     else if(n>strlen(s2)){
    36         for(i=0; i<strlen(s2); i++)
    37             *(s1+i)=*(s2+i);
    38         while(i<n){
    39             *(s1+i)='';
    40             i++;
    41         }
    42     }
    43     return s1;
    44 }

    参考答案给的strncpy()定义如下:

     1 char *mystrncpy(char *p1, char *p2, int n)
     2 {  
     3    char *p=p1;  
     4    while(*p1++ != '') continue; 
     5    *--p1 = *p2; 
     6    n--;  
     7    while(n>0 && *p2 != '') {   
     8        *++p1 = *++p2;  
     9        n--; 
    10    } 
    11    return p;
    12
  • 相关阅读:
    1697 ⑨要写信
    1220 数字三角形
    4979 数塔
    bzoj1618[Usaco2008 Nov]Buying Hay 购买干草
    bzoj1066[SCOI2007]蜥蜴
    bzoj1008[HNOI2008]越狱
    cf437D The Child and Zoo
    cf437C The Child and Toy
    cf437B The Child and Set
    cf437A The Child and Homework
  • 原文地址:https://www.cnblogs.com/coding-time/p/4526645.html
Copyright © 2011-2022 走看看