《c primer plus》编程练习回顾-第十一章(上)

1.

#include
void input(char *p, int b);
int main(void)
{
    char ch[60];
    int n;
    printf("Please input the number:");
    scanf("%d", &n);
    getchar();
    input(ch, n);
    puts(ch);
    return 0;
}
void input(char *p, int b)
{
    int i;
    for (i = 0; i < b; i++)
        p[i] = getchar();
    p[i] = '\0';
}

2.

#include
#include
void input(char *p, int b);
int main(void)
{
    char ch[60];
    int n;
    printf("Please input the number:");
    scanf("%d", &n);
    getchar();
    input(ch, n);
    puts(ch);
    return 0;
}
void input(char *p, int b)
{
    int i;
    for (i = 0; i < b; i++)
    {
        p[i] = getchar();
        if (isspace(p[i]))
            break;
    }
    p[i] = '\0';
}

3.

#include
#include
#include
void fun(void);
int main(void)
{
    fun();
    return 0;
}
void fun(void)
{
    char a[40],ch;
    int i = 0;
    bool in_word=true;
    while (in_word && (ch=getchar()))
    {

        if (isalpha(ch))
        {
            a[i] = ch;
            i++;
            in_word = false;
        }
    }
    while (in_word == 0 && (ch=getchar()))
    {
        if (!isalpha(ch))
        {
            in_word = 1;
            a[i] = '\0';
            i++;
        }
        a[i] = ch;
        i++;
    }
    printf("%s\n", a);
}

4.

#include
#include
char *fun(char *, char);
int main(void)
{
    char str[81], ch;
    char *p;
    printf("Please input the string:");
    gets(str);
    printf("Plese input a char:");
    scanf("%c", &ch);
    p = fun(str, ch);
    if (p)
    {
        printf("Find!");
    }
    else
        puts("Can't find");
    return 0;
}
char *fun(char *a, char b)
{
    int i;
    char *q;
    for (i = 0; i <= strlen(a); i++)
    {
        if (*(a + i) == b)
        {
            q = a + i;
            return q;
        }
    }
    if (i == (strlen(a)+1))
        q = NULL;
    return q;
}

5.

#include
#include
int wit_lun(char *, char);
int main(void)
{
    char str[81], ch;
    int p;
    printf("Please input a string:");
    gets(str);
    printf("Please input a char:");
    scanf("%c", &ch);
    p = wit_lun(str, ch);
    if (p)
        printf("Find\n");
    else
        printf("Can't find\n");
    return 0;
}
int wit_lun(char *b, char c)
{
    int i;
    char *q;
    for (i = 0; i <= strlen(b); i++)
        if (*(b + i) == c)
            return 1;
        if (i == strlen(b) + 1)
            return 0;
}

发现自己水平还是差很多,这章内容比较难,分三个部分写或许负担会轻些。

你可能感兴趣的:(c,primer,plus编程练习)