C 每行打印一个字
我需要写一个程序,每行打印一个字。以下是我目前得到的信息:C 每行打印一个字,c,string,line,C,String,Line,我需要写一个程序,每行打印一个字。以下是我目前得到的信息: #include <stdio.h> main(){ int c; while ((c = getchar()) != EOF){ if (c != ' ' || c!='\n' || c!='\t') printf("%c", c); else printf("\n"); } } 它把整个东西都打印出来了。这里出了什
#include <stdio.h>
main(){
int c;
while ((c = getchar()) != EOF){
if (c != ' ' || c!='\n' || c!='\t')
printf("%c", c);
else
printf("\n");
}
}
它把整个东西都打印出来了。这里出了什么问题?如果(c!=''c!='\n''c!='\t')
这永远不会是错误的
也许你的意思是:
if(c!='&&c!='\n'&&c!='\t')
如果需要string.h
库中的strtok
函数,可以使用该函数,该函数可以通过提供分隔符将输入剪切成许多单词
这里是一个完美的代码注释,可以满足您的需要
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char line[1000]=""; // the line that you will enter in the input
printf("Input the line:\n>>");
scanf("%[^\n]",line); // read the line till the you hit enter button
char *p=strtok(line," !#$%&'()*+,-./'"); // cut the line into words
// delimiter here are punctuation characters (blank)!#$%&'()*+,-./'
printf("\nThese are the words written in the line :\n");
printf("----------------------------------------\n");
while (p!=NULL) // a loop to extract the words one by one
{
printf("%s\n",p); // print each word
p=strtok(NULL," !#$%&'()*+,-./'"); // repeat till p is null
}
return 0;
}
与使用printf try putchar不同,同样根据上面的注释,您应该使用&&而不是| | 这是我的密码-
#include<stdio.h>
main()
{
int c, nw; /* nw for word & c for character*/
while ( ( c = getchar() ) != EOF ){
if ( c != ' ' && c != '\n' && c != '\t')
nw = c;
else {
nw = '\n';
}
putchar (nw);
}
}
#包括
main()
{
int c,nw;/*nw表示单词,c表示字符*/
而((c=getchar())!=EOF){
如果(c!=''&&c!='\n'&&c!='\t')
nw=c;
否则{
nw='\n';
}
普查尔(西北);
}
}
此代码将为您提供所需的输出我认为简单的解决方案如下
#include <stdio.h>
int main(void) {
// your code goes here
int c;
while((c=getchar())!=EOF)
{
if(c==' ' || c=='\t' || c=='\b')
{
printf("\n");
while(c==' ' || c=='\t' || c=='\b')
c=getchar();
}
if(c!=EOF)
putchar(c);
}
return 0;
}
#包括
内部主(空){
//你的密码在这里
INTC;
而((c=getchar())!=EOF)
{
如果(c=''| | c='\t'| | c='\b')
{
printf(“\n”);
而(c=''| | c='\t'| | c='\b')
c=getchar();
}
如果(c!=EOF)
普查尔(c);
}
返回0;
}
查看putchar
。(printf
不是打印东西的唯一方法,有时也不是最好的方法。)此外,问问自己当单词之间有多个空格时会发生什么。您可能想查看scanf
(或fscanf
)以获取输入。它知道如何跳过空白并给出下一个单词。建议while((c=getchar())!=EOF){if(!isspace(c)){printf(“%c”,c);否则…
主函数的布局是:int main(){…return(0);}编译/链接时需要启用所有警告。(需要更正警告)这行:'if(c!=''c!='\n'.''c!='\t')应该是:'if(c!=''&&c!='\n'&&c!='\t')更改的原因。.如果其中任何一个比较为'true',则打印字符。通过使用'&&',所有比较必须为'tgrue'以打印字符。如果用户输入“\n”
,行
未初始化,代码继续执行未定义的行为。请说明未定义行为的来源!我就在这里说过:“如果用户输入“\n”,行未初始化”。当行
处于未定义状态时,strok(line…
导致UB。您认为当用户输入“\n”
”时,line
会发生什么情况?它将包含垃圾,请更具体一些“\n”和ctrl-d是两种不同的东西!感谢您提供的信息!您完全正确!最好使用fgets()
。在标记时,fgets()
与scanf(“%[^\n]”)带来的典型尾随'\n'
将不会成为问题。
然后再加上code可以很好地处理输入“\n”
和长输入。
suggest the code implement a state machine,
where there are two states, in-a-word and not-in-a-word.
Also, there are numerous other characters that could be read
(I.E. ',' '.' '?' etc) that need to be check for.
the general logic:
state = not-in-a-word
output '\n'
get first char
loop until eof
if char is in range a...z or in range A...Z
then
output char
state = in-a-word
else if state == in-a-word
then
output '\n'
state = not-in-a-word
else
do nothing
end if
get next char
end loop
output '\n'
#include<stdio.h>
main()
{
int c, nw; /* nw for word & c for character*/
while ( ( c = getchar() ) != EOF ){
if ( c != ' ' && c != '\n' && c != '\t')
nw = c;
else {
nw = '\n';
}
putchar (nw);
}
}
#include <stdio.h>
int main(void) {
// your code goes here
int c;
while((c=getchar())!=EOF)
{
if(c==' ' || c=='\t' || c=='\b')
{
printf("\n");
while(c==' ' || c=='\t' || c=='\b')
c=getchar();
}
if(c!=EOF)
putchar(c);
}
return 0;
}