用C语言编程如何将输入的字符串输出到屏幕上?

用C语言编程如何将输入的字符串输出到屏幕上?,c,string,loops,C,String,Loops,我试图让我的程序将输入到程序中的文字输出到屏幕上。到目前为止,我的程序根据我键入的内容输出随机字符。例如,如果我输入单词hey,它将在屏幕上输出%。我如何解决这个问题,在屏幕上输出单词hey?我的代码在下面 #include <stdio.h> #include<conio.h> int main(){ int word; char cont; for (;;){ int countword = 0; int co

我试图让我的程序将输入到程序中的文字输出到屏幕上。到目前为止,我的程序根据我键入的内容输出随机字符。例如,如果我输入单词hey,它将在屏幕上输出%。我如何解决这个问题,在屏幕上输出单词hey?我的代码在下面

#include <stdio.h>
#include<conio.h>

int main(){
    int word;
    char cont;
    for (;;){
        int countword = 0;
        int countpunct = 0;
        printf("\nEnter the String: ");
        while ((word = getchar()) != EOF && word != '\n'){
            if (word == ' ' || word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
                countword++;
            }
            if (word == '.' || word == '?' || word == '!' || word == '(' || word == ')' || word == '*' || word == '&'){
                countpunct++;
            }
        }

        printf("%c", word);

        printf("\nThe number of words is %d.", countword);

        printf("\nThe number of punctuation marks is %d.", countpunct);

        printf("\nContinue? Y/N?");
        scanf("%c", &cont);
        if (cont != 'y' && cont != 'Y'){
            return 0;
        }
    }
}
#包括
#包括
int main(){
int字;
字符控制;
对于(;;){
int countword=0;
int countpunct=0;
printf(“\n输入字符串:”);
而((word=getchar())!=EOF&&word!='\n'){
如果(单词=“”| |单词=“”.| |单词=“”?“”| |单词=“”!“”| |单词=“”(‘| |单词=“”)‘| |单词=“”*‘| |单词=“”&’){
countword++;
}
如果(单词=='。|单词=='?'|单词=='!'|单词=='('|单词==')'|单词=='*'|单词=='&'){
countpunct++;
}
}
printf(“%c”,单词);
printf(“\n字数为%d.”,countword);
printf(“\n标点符号的数量为%d.”,countpunct);
printf(“\N是否继续?”);
scanf(“%c”,续(&c));
if(cont!=“y”&&cont!=“y”){
返回0;
}
}
}

您正在将
&
printf()一起使用。它将打印变量的地址(而不是它的值)

改为这样做:

printf("%c", word); // notice there is no &
除此之外,我注意到您的代码中有几点值得一提:


word
声明为
int
,但读取和打印为
char
。为什么?

您的
while
循环确保
word
在退出
while
循环时保持
EOF
\n

while ((word = getchar()) != EOF && word != '\n')

您没有打印
word
的值。使用
printf(“%c”,单词)。此外,没有必要将其类型设置为
int
。如果您以前没有使用过调试器,现在可能是时候了。逐行逐行浏览代码,同时跟踪所有变量及其更改,如果您想打印每个字符,请将
printf
移动到循环中,这样我就可以让我的程序将输入的每个单词打印到新行?记住
getchar
返回一个
int
。而那
(char)EOF!=(int)EOF