在调用函数时添加重复
adding repetition to calling a function
好的,我正在尝试制作一个代码,用字母表中的下一个字符替换输入的字符串中的字符,并将其打印 26 次(所有字母都旋转)
我从 rot13 代码中获得了帮助,对它进行了一些修改,除了打印 26 次之外,它可以做我想做的一切。我试着用 for 和 while 添加一个计数器,但我真的看起来很愚蠢,当然没有用。
这是代码
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Please Enter The Secret Code\n");
int code;
while((code = getchar())) {
code = chariot(code);
putchar(code);
}
return 0;
}
int chariot(int code)
{
if('a' <= code && code <= 'z'){
return Chariot(code,'a');
} else if ('A' <= code && code <= 'Z') {
return Chariot(code, 'A');
} else {
return code;
}
}
int Chariot(int code, int newcode){
code = (((code-newcode)+1)%26)+newcode;
return code;
}
你需要做的是运行最后一点:
int Chariot(int code, int new code)
{
code = (((code-newcode)+1)%26)+newcode;
return code;
}
通过 'for' 循环。
它应该看起来像这样:
int Chariot(int code, int new code)
{
for (int i = 0; i < 26; i++)
{
code = (((code-newcode)+1)%26)+newcode;
}
return code;
}
这样做是 运行将同一段代码重复 26 次,这就是您想要的结果。对循环做更多的研究,因为它们会极大地帮助你的生活。
如果你坚持一个字符一个字符地处理输入,你可以在足够高的VT100兼容终端上使用这个:
while (code = getchar())
{
int i;
for (i = 0; i < 26; ++i)
if (putchar(code = chariot(code)) != '\n') printf("\b\v");
if (code != '\n') printf(" \e[26A");
}
当然,这很荒谬。理智的解决方案是 o_weisman 建议的:
read the entire string first, and only then run Chariot function on
all its characters in a loop 26 times printing the result after each
call.
好的,我正在尝试制作一个代码,用字母表中的下一个字符替换输入的字符串中的字符,并将其打印 26 次(所有字母都旋转)
我从 rot13 代码中获得了帮助,对它进行了一些修改,除了打印 26 次之外,它可以做我想做的一切。我试着用 for 和 while 添加一个计数器,但我真的看起来很愚蠢,当然没有用。
这是代码
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Please Enter The Secret Code\n");
int code;
while((code = getchar())) {
code = chariot(code);
putchar(code);
}
return 0;
}
int chariot(int code)
{
if('a' <= code && code <= 'z'){
return Chariot(code,'a');
} else if ('A' <= code && code <= 'Z') {
return Chariot(code, 'A');
} else {
return code;
}
}
int Chariot(int code, int newcode){
code = (((code-newcode)+1)%26)+newcode;
return code;
}
你需要做的是运行最后一点:
int Chariot(int code, int new code)
{
code = (((code-newcode)+1)%26)+newcode;
return code;
}
通过 'for' 循环。
它应该看起来像这样:
int Chariot(int code, int new code)
{
for (int i = 0; i < 26; i++)
{
code = (((code-newcode)+1)%26)+newcode;
}
return code;
}
这样做是 运行将同一段代码重复 26 次,这就是您想要的结果。对循环做更多的研究,因为它们会极大地帮助你的生活。
如果你坚持一个字符一个字符地处理输入,你可以在足够高的VT100兼容终端上使用这个:
while (code = getchar())
{
int i;
for (i = 0; i < 26; ++i)
if (putchar(code = chariot(code)) != '\n') printf("\b\v");
if (code != '\n') printf(" \e[26A");
}
当然,这很荒谬。理智的解决方案是 o_weisman 建议的:
read the entire string first, and only then run Chariot function on all its characters in a loop 26 times printing the result after each call.