首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >scanf使用空格作为行尾

scanf使用空格作为行尾
EN

Stack Overflow用户
提问于 2018-08-31 05:40:25
回答 1查看 547关注 0票数 1

为了我的夏季项目,我构建了一个俗气的小加密引擎,我想有一个功能,用户可以输入一个字符串/字符序列,它会在终端上对其进行加密,但由于某种原因,程序(我认为它的scanf)只打印出第一个单词。这可能是我确定用户输入结束的方式,但我认为问题出在scanf,而不是我的代码(尽管我读了这篇文章,它让我对自己产生了疑问)。下面是相关的代码:

代码语言:javascript
复制
printf("would you like to [e]ncrypt or [d]ecrypt, or type a word to encrypt\n");
char q[50];
for(int s = 0; s<50; s++){ //initialize string to '0'
    q[s] = '0';
}
scanf("%s \n", q); //reads input from user
for(int s = 0; s<50; s++){ //for loop to read every char and encrypt it
        if(q[s] == '0'){ //'0' determines the end of line
            break;
        }else{  
            union shifter sh = { 0 }; //uses a union to shift the char to unreadable (relatively) char
            sh.str = q[s];
            sh.i += 5;
            printf("%c", sh.str); //prints out chars one at a time
        }

    }
printf("\n"); //new line when done

你们说用户不能输入0,我知道这是一个次要问题,我可以通过说inb4将其初始化为不可读的字符吗?

代码语言:javascript
复制
char = 3;

并将其作为不可读的字符(准确地说是测试结束)

谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2018-08-31 05:43:37

您的%s将只匹配非空格字符的第一个字符串,然后丢弃第一个空格之后的其他字符。

请参阅http://www.cplusplus.com/reference/cstdio/scanf/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52105729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档