阅读输入问题 [英] Reading input problem

查看:136
本文介绍了阅读输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为getword的函数,它读取std。

输入的每一个输入。


我想要的是什么:我希望它读取如果它有少于或等于30

个字符。除此之外的任何其他内容都应该被丢弃,并且它应该向用户询问新的输入。 Ctrl-D应退出程序。


我的GOT:它读取超过30个字符的内容作为下一个字:\和

我必须按两次Ctrl-D退出程序。

/ *这个程序只会创建一个指向整数的指针数组

*并将填充一些值而使用malloc创建

*指针来填充数组然后将打印指向的值

*由这些指针

*

*版本1.1

*

* /


#include< stdio.h>

#include< ctype.h>

enum {MAX = 30};


int getword(char *,int);

int main(无效)

{

char word [MAX];


while(getword(单词,MAX))

{

printf(" ----------%s\ n",word);

}


返回0;

}


/ *一个单词的程序就像我一样NPUT。如果它包含26个字母以外的任何其他内容,它将丢弃

*整个输入。

* of English。如果输入的单词包含超过30个字母,那么只有

*将丢弃额外的字母。对于

*英语的一般用途,使用大于这个大小的单词没有任何意义。

*几乎每个通用单词都可以用单词表示少于
*超过或等于30个字母。

*

* /

int getword(char * word,int max_length)

{

int c;

char * w = word;

while(isspace (c = getchar()))

{

;

}


if(isalpha) (c))

{

* w ++ = c;

}


while( - -max_length&&(c = getchar()))

{

if(isalpha(c))

{

* w ++ = c;

}

else if(isspace(c)|| c == EOF)

{

* w =''\ 0'';

休息;

}

其他

{

返回0;

}

}

* w =''\ 0'';

返回单词[0];

}


================输出=====================

[arnuld @ raj C] $ gcc -ansi -pedantic -Wall -Wextra test.c

[arnuld @ raj C] $ ./a.out

like

----------像



----------这

lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqpppppppppddddddddddddddddddddddddddddddddddddddddddddddd ...
---------- lllllllllllllllpppppppppppppqq

---------- qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

- -------- qqqqqqqqqpppppppppppdddddddddd

---------- dddddddddddddddddddddd

[arnuld @ raj C] $


-
http://lispmachine.wordpress。 com /

我的电子邮件ID是@上面的博客。

只需查看关于我自己页面:)

I have a function named getword that read every single input from std.
input.

WHAT I WANTED: I want it read the word if it has less than or equal to 30
characters. Anything else beyond that should be discarded and it should
ask the user for new input. Ctrl-D should exit from the program.

WHAT I GOT: It reads anything beyond 30 characters as the next word :\ and
I have to press Ctrl-D two times to exit from the program.
/* This program will simply create an array of pointers to integers
* and will fill it with some values while using malloc to create
* pointers to fill the array and then will print the values pointed
* by those pointers
*
* version 1.1
*
*/

#include <stdio.h>
#include <ctype.h>
enum { MAX = 30 };

int getword( char *, int );
int main( void )
{
char word[MAX];

while( getword( word, MAX ) )
{
printf( "---------- %s\n", word );
}

return 0;
}

/* A program that takes a single word as input. It will discard
* the whole input if it contains anything other than the 26 alphabets
* of English. If the input word contains more than 30 letters then only
* the extra letters will be discarded . For general purpose usage of
* English it does not make any sense to use a word larger than this size.
* Nearly every general purpose word can be expressed in a word with less
* than or equal to 30 letters.
*
*/
int getword( char *word, int max_length )
{
int c;
char *w = word;
while( isspace( c = getchar() ) )
{
;
}

if( isalpha( c ) )
{
*w++ = c;
}

while( --max_length && (c = getchar()) )
{
if( isalpha( c ) )
{
*w++ = c;
}
else if( isspace( c ) || c == EOF)
{
*w = ''\0'';
break;
}
else
{
return 0;
}
}
*w = ''\0'';
return word[0];
}

================ OUTPUT =====================
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@raj C]$ ./a.out
like
---------- like
this
---------- this
lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqpppppppppppdddddddddddddddddddd dddddd
---------- lllllllllllllllpppppppppppppqq
---------- qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
---------- qqqqqqqqqpppppppppppdddddddddd
---------- dddddddddddddddd
[arnuld@raj C]$


--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)

推荐答案

gcc -ansi -pedantic -Wall -Wextra test.c

[arnuld @raj C]
gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@raj C]


./ a.out

喜欢

----------喜欢

。这种

----------此

lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqpppppppppppdddddddddddddddddddd DDDDDD

----- ----- lllllllllllllllpppppppppppppqq

---------- qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

---------- qqqqqqqqqpppppppppppdddddddddd

---------- ddddddddddddddddddd

[arnuld @ raj C]
./a.out
like
---------- like
this
---------- this
lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqpppppppppppdddddddddddddddddddd dddddd
---------- lllllllllllllllpppppppppppppqq
---------- qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
---------- qqqqqqqqqpppppppppppdddddddddd
---------- dddddddddddddddd
[arnuld@raj C]





-
http://lispmachine.wordpress.com/

我的电子邮件ID是@上面的博客。

只需查看关于我自己页面:)




--
http://lispmachine.wordpress.com/
my email ID is @ the above blog.
just check the "About Myself" page :)


这篇关于阅读输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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