c - 如何控制数组的长度
问题描述
我的问题是数组 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 个”
解决方案
scanf 由于捕获换行符的问题,您想要做的事情很困难。换行符被捕获为 char 数组的一部分
。fgets
只需检查输入是否包含换行符,您就知道没有更多字符可供读取。
有一些有用的函数string.h
可以使这段代码更短,例如strchr
和strcspn
。
#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;
}
推荐阅读
- asp.net - 母版页不在会话中时出现序列化错误
- mysql - 即使另一个孩子将其作为父母,ON DELETE CASCADE 也会删除父母吗?
- javascript - jQuery:如何以简洁的方式编写 if hasClass 语句
- spring - 当我不调用 thymeleaf 时,thymeleaf 出错
- javascript - 如何打印特定的div?
- c# - 当一个属性有重复项时重新排列 C# 对象列表
- python - 如何使用函数将 2 个列表组合成 1 个字典
- flutter - 如何使用getx制作相机屏幕?
- php - 如何在 Drupal 代码中显示商店的渲染实体?
- google-app-engine - 如何使用 App Engine 处理谷歌建议 API 配额问题?