如何获得在C standardinput的长度? [英] How to get the length of a standardinput in C?

查看:140
本文介绍了如何获得在C standardinput的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将与我的code启动:

I will start with my code:

char input[40];
fgets( input, 40, stdin );
if( checkPalin(input) == 0 ) {
    printf("%s ist ein Palindrom \n", input);
}
else {
    printf("%s ist kein Palindrom \n", input);
}

我想要做的是:看了一些standardinput和我的功能检查,如果它是一个回文与否。

What I want to do is: Read some standardinput and check with my function if it is a Palindrome or not.

我的问题有以下几种:
我怎样才能获得standardinput的长度?因为如果是大则40个字符我想把一个errormessage的,此外我希望我的字符数组是实际输入的准确长度。

My problems are the following: How can I get the length of the standardinput? Because if it is larger then 40 chars I wanna put an errormessage and furthermore I want my char array to be the exact length of the actual input.

任何人都可以帮我吗?

推荐答案

与fgets(输入,40,标准输入);

输入的长度不得超出40个字符== 39characters +空字符

length of input should not go beyond 40 characters == 39characters + nul character

如果你给出具有字符串长度超过39个字符,那么与fgets()读第39个字符和地点空字符('\\ 0' ) 40字符并忽略其余的字符。

If you give string having length more than 39 characters, then fgets() reads first 39 characters and place nul character('\0') as 40 character and ignores remaining characters.

如果你给字符串少于39个字符,例如5
然后,它会读取也换行
长度变​​成6(不包括空字符)

If you give string less than 39 characters , for example 5 then it places reads newline also length becomes 6(excluding nul character)

不要忘了删除换行符。

char input[60];
fgets(input,sizeof input,stdin);

例如,如果你声明与一些60后输入缓冲区的大小,如果你想要做的错误检查超过40个字符。

For example if you declare input buffer size with some 60 then if you want to do error checking for more than 40 characters.

您可以简单地用检查的strlen(),并检查长度超过40.then显示错误信息

You can simply check with strlen() and check length is more than 40.then show error message

如果你想与与fgets()对证 NULL

这篇关于如何获得在C standardinput的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆