C PRIMER PLUS第六版 第十一章编程练习

1.

#include <stdio.h>
#define SIZE 40
void s_gets(char str[], int n);

int main(void)
{
    int i;
    int num;
    char st [SIZE];

    printf("Enter a number for the length of the string:");
    scanf("%d",&num);
    while(getchar() != '\n')
        continue;
    s_gets(st,num);
    fputs(st,stdout);
    printf("\n");
    for (i = 0; i < num; ++i) {
        printf("%d\n",st[i]);
    }

    return 0;
}

void s_gets(char str[], int n)
{
    printf("Enter a string:");
    fgets(str,n,stdin);
}

2.

    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 40
    void s_gets(char str[], int n);

    int main(void)
    {
        int i;
        int num;
        char st [SIZE];

        printf("Enter a number for the length of the string:");
        scanf("%d",&num);
        while(getchar() != '\n')
            continue;
        s_gets(st,num);
        puts(st);
        printf("\n");
        for (i = 0; i < num; i++) {
            printf("%d\n",st[i]);
        }

        return 0;
    }

    void s_gets(char str[], int n)
    {
        int i;
        printf("Enter a string:");
        for (i = 0; i < n; i++) {
            str[i] = getchar();
            if(isspace(str[i])) {
                str[i] = '\0';
                break;
            }
        }
    }

3.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 80
void s_gets(char * st);

int main(void)
{
    char str [SIZE];
    int length;
    int i;

    printf("Enter a string, we will get the first word:");
    s_gets(str);
    length = strlen(str);
    for(i = 0; i < length; i++)
        printf("%c",str[i]);

    return 0;
}

void s_gets(char * st)
{
    int i = 0;
    int j;

    while(isspace(st[i] = getchar()))
        continue;
    for(j = 1; ; j++)
    {
        st[j] = getchar();
        if(isspace(st[j]))
        {
            st[j] = '\0';
            break;
        }
    }
}

4.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 80
void s_gets(char * st, int n);

int main(void)
{
    char str [SIZE];
    int length;
    int i,max;

    printf("Enter the up limit of the letters in the word:");
    scanf("%d",&max);
    printf("Enter a string, we will get the first word:");
    s_gets(str,max);
    length = strlen(str);
    for(i = 0; i < length; i++)
        printf("%c",str[i]);

    return 0;
}

void s_gets(char * st, int n)
{
    int i = 0;
    int j;

    while(isspace(st[i] = getchar()))
        continue;
    for(j = 1; j < n; j++)
    {
        st[j] = getchar();
        if(isspace(st[j]))
        {
            st[j] = '\0';
            break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/o707191418/article/details/81511945