首页 > 解决方案 > 如何控制数组的长度

问题描述

我的问题是数组 b 可以占用超过 12 个字符,如果发生这种情况,它只会打印出数组 b 而没有错误

{
int i=12;
char a[i] ,b[i];
printf("give string with no more than 12 characters ");
scanf("%s",a);
printf("give second string with no more than 12 characters\n");
scanf("%s",b);
if(a[i]>i || b[i]>i){
    printf("try again cause you typed more than 12\n");
    printf("first \n");
    scanf("%s",a);
    printf("second \n");
    scanf("%s",b);
    }
printf("you gave %s & %s \n",a,b);

当我输入超过 12 个字符时,我希望输出“再试一次,因为您输入了超过 12 个”

标签: c

解决方案


scanf 由于捕获换行符的问题,您想要做的事情很困难。换行符被捕获为 char 数组的一部分
fgets只需检查输入是否包含换行符,您就知道没有更多字符可供读取。
有一些有用的函数string.h可以使这段代码更短,例如strchrstrcspn

#include <stdio.h>

int main( void) {
    char a[14];
    char b[14];
    int toolong = 1;

    do {
        int retry = 0;//not retry
        toolong = 1;//too long
        printf ( "give string with no more than 12 characters ");
        if ( fgets ( a, sizeof a, stdin)) {//read a line
            int each = 0;
            while ( a[each]) {
                if ( '\n' == a[each]) {//found a newline
                    if ( ! retry) {//not retry
                        toolong = 0;//not too long
                        a[each] = 0;//remove newline
                    }
                    else {
                        printf ( "try again cause you typed more than 12\n");
                    }
                    break;
                }
                each++;
                if ( ! a[each]) {//found zero terminator at end of line
                    if ( ! fgets ( a, sizeof a, stdin)) {//read more to find newline
                        fprintf ( stderr, "fgets EOF\n");
                        return 0;
                    }
                    each = 0;//start over at [0]
                    retry = 1;//retry
                }
            }
        }
        else {
            fprintf ( stderr, "fgets EOF\n");
            return 0;
        }
    } while ( toolong);

    do {
        int retry = 0;
        toolong = 1;
        printf ( "give second string with no more than 12 characters ");
        if ( fgets ( b, sizeof b, stdin)) {
            int each = 0;
            while ( b[each]) {
                if ( '\n' == b[each]) {
                    if ( ! retry) {
                        toolong = 0;
                        b[each] = 0;
                    }
                    else {
                        printf ( "try again cause you typed more than 12\n");
                    }
                    break;
                }
                each++;
                if ( ! b[each]) {
                    if ( ! fgets ( b, sizeof b, stdin)) {
                        fprintf ( stderr, "fgets EOF\n");
                        return 0;
                    }
                    each = 0;
                    retry = 1;
                }
            }
        }
        else {
            fprintf ( stderr, "fgets EOF\n");
            return 0;
        }
    } while ( toolong);

    printf ( "you gave %s & %s\n", a, b);

    return 0;
}

使用string.h重复代码并将其放入函数中可以使其更短。

#include <stdio.h>
#include <string.h>

char *strlimit ( char *str, int limit) {
    int toolong = 1;

    do {
        toolong = 0;// not too long
        printf ( "give string with no more than 12 characters ");
        if ( fgets ( str, limit, stdin)) {//read a line
            if ( ! strchr ( str, '\n')) {//no newline
                while ( ! strchr ( str, '\n')) {//look for newline
                    fgets ( str, limit, stdin);//read more
                }
                toolong = 1;// too long
                printf ( "try again cause you typed more than 12\n");
            }
            str[strcspn ( str, "\n")] = 0;//remove newline
        }
        else {
            fprintf ( stderr, "fgets EOF\n");
            return NULL;
        }
    } while ( toolong);

    return str;
}

int main( void) {
    char a[14];
    char b[14];

    if ( ! strlimit ( a, sizeof a)) {
        return 0;
    }

    if ( ! strlimit ( b, sizeof b)) {
        return 0;
    }

    printf ( "you gave %s & %s\n", a, b);

    return 0;
}

推荐阅读