为了我的夏季项目,我构建了一个俗气的小加密引擎,我想有一个功能,用户可以输入一个字符串/字符序列,它会在终端上对其进行加密,但由于某种原因,程序(我认为它的scanf)只打印出第一个单词。这可能是我确定用户输入结束的方式,但我认为问题出在scanf,而不是我的代码(尽管我读了这篇文章,它让我对自己产生了疑问)。下面是相关的代码:
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将其初始化为不可读的字符吗?
char = 3;
并将其作为不可读的字符(准确地说是测试结束)
谢谢你的帮助
发布于 2018-08-31 05:43:37
您的%s
将只匹配非空格字符的第一个字符串,然后丢弃第一个空格之后的其他字符。
https://stackoverflow.com/questions/52105729
复制相似问题