如何在 c 中使用 for 循环在一行中打印全文?
how to print full text in one line using for loop in c?
我正在使用 for 循环将字符串中的每个字符加一个并打印出来,但是当打印它们时,每个字母都在一个新行上形成,顺便说一句,我是 C 语言的初学者。任何慷慨的帮助将不胜感激。顺便说一句,由于 for 循环的工作原理,我知道每个字母都会被打印出来,但我不知道如何修复它
for (int i = 0; i < strlen(text); i++)
{
char c = plaintext[i];
printf("CipherText: %c\n", c + 1);
}
这是输出:
Plaintext: hello
CipherText: i
CipherText: f
CipherText: m
CipherText: m
CipherText: p
printf("CipherText: );
for (int i = 0; i < strlen(text); i++)
{
char c = plaintext[i];
printf(" %c", c + 1);
}
只需删除 \n
(换行符)字符。
(以及其他一些建议。)
int main(void)
{
char plaintext[] = "this is original cipher";
char c = 0;//create this before entering loop
size_t len = strlen(plaintext);//assign length to variable that can be used in for statement
printf("%s", "CipherText: ");//print this before entering loop
for (int i = 0; i < len; i++)
{
c = plaintext[i];//output each char in sequence
printf("%c", c + 1);//output each char in sequence
}
printf("%s", "\n");//Optional = moves the cursor to the next line in case
//there are follow-on lines to printf
getchar();
return 0;
}
对于给定的示例 'plaintext',这里是输出:
您也可以用这种简短的方式完成:
#include <stdio.h>
int main(void) {
char str[] = "hello";
printf("PlainText: %s\nCipherText: ", str);
for( int i = 0; str[i] != '[=10=]'; i++)
putchar(str[i] + 1);
return 0;
}
输出:
PlainText: hello
CipherText: ifmmp
每个以 \
开头的字符都称为转义字符,当您打印加密文本时,每次打印时都带有一个转义字符 \n
,称为 换行个字符。这就是为什么密文中的每个字符都被打印在一个新行上的原因。只需从 printf("CipherText: %c\n", c + 1);
中删除 \n
即可得到预期的输出。
您还可以了解有关转义字符的更多信息on this Wikipedia page about them。
这里有一个更短的版本:
printf("Ciphertext:");
for (const char *i = plaintext; *i; i++) printf("%c", *i + 1);
或者没有指针
printf("Ciphertext:");
for (size_t index = 0; plaintext[index]; index++) printf("%c", plaintext[index] + 1);
您可以在循环中使用 putchar
而不是 printf
,但大多数优化编译器会为您完成
您必须从 for 循环中删除 \n(换行符)字符。与 Python 不同,C 不会添加新行,除非您添加 \n.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string plaintext = get_string("plaintext: "); // asking user to enter a word
for (int i = 0, n = strlen(plaintext); n > i ; i++) // looping through each entered letter
{
printf("%c" , plaintext[i] + 1); // changing the letter by 1 place
}
printf("\n");
}
我正在使用 for 循环将字符串中的每个字符加一个并打印出来,但是当打印它们时,每个字母都在一个新行上形成,顺便说一句,我是 C 语言的初学者。任何慷慨的帮助将不胜感激。顺便说一句,由于 for 循环的工作原理,我知道每个字母都会被打印出来,但我不知道如何修复它
for (int i = 0; i < strlen(text); i++)
{
char c = plaintext[i];
printf("CipherText: %c\n", c + 1);
}
这是输出:
Plaintext: hello
CipherText: i
CipherText: f
CipherText: m
CipherText: m
CipherText: p
printf("CipherText: );
for (int i = 0; i < strlen(text); i++)
{
char c = plaintext[i];
printf(" %c", c + 1);
}
只需删除 \n
(换行符)字符。
(以及其他一些建议。)
int main(void)
{
char plaintext[] = "this is original cipher";
char c = 0;//create this before entering loop
size_t len = strlen(plaintext);//assign length to variable that can be used in for statement
printf("%s", "CipherText: ");//print this before entering loop
for (int i = 0; i < len; i++)
{
c = plaintext[i];//output each char in sequence
printf("%c", c + 1);//output each char in sequence
}
printf("%s", "\n");//Optional = moves the cursor to the next line in case
//there are follow-on lines to printf
getchar();
return 0;
}
对于给定的示例 'plaintext',这里是输出:
您也可以用这种简短的方式完成:
#include <stdio.h>
int main(void) {
char str[] = "hello";
printf("PlainText: %s\nCipherText: ", str);
for( int i = 0; str[i] != '[=10=]'; i++)
putchar(str[i] + 1);
return 0;
}
输出:
PlainText: hello
CipherText: ifmmp
每个以 \
开头的字符都称为转义字符,当您打印加密文本时,每次打印时都带有一个转义字符 \n
,称为 换行个字符。这就是为什么密文中的每个字符都被打印在一个新行上的原因。只需从 printf("CipherText: %c\n", c + 1);
中删除 \n
即可得到预期的输出。
您还可以了解有关转义字符的更多信息on this Wikipedia page about them。
这里有一个更短的版本:
printf("Ciphertext:");
for (const char *i = plaintext; *i; i++) printf("%c", *i + 1);
或者没有指针
printf("Ciphertext:");
for (size_t index = 0; plaintext[index]; index++) printf("%c", plaintext[index] + 1);
您可以在循环中使用 putchar
而不是 printf
,但大多数优化编译器会为您完成
您必须从 for 循环中删除 \n(换行符)字符。与 Python 不同,C 不会添加新行,除非您添加 \n.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string plaintext = get_string("plaintext: "); // asking user to enter a word
for (int i = 0, n = strlen(plaintext); n > i ; i++) // looping through each entered letter
{
printf("%c" , plaintext[i] + 1); // changing the letter by 1 place
}
printf("\n");
}