【问题标题】:how to print full text in one line using for loop in c?如何使用c中的for循环在一行中打印全文?
【发布时间】:2020-06-27 14:01:45
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: c for-loop cs50


    【解决方案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;
    }
    

    对于给定的示例“纯文本”,输出如下:

    【讨论】:

      【解决方案2】:

      这里有一个更短的版本:

      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,但大多数优化编译器都会为您完成

      【讨论】:

      • @ryyker 然后没有指针版本 - 但正确。使用正确的类型,没有不必要的字符串遍历
      • 好吧,诚然,这是一个很好的解决方案。但由于我通常不做嵌入式,或者任何需要避免使用带有字符串长度的for() 循环作为退出标准的非常惯用的构造,我更喜欢看到(和使用)字符串遍历 在代码中,即使它是不必要的。而且,如果您仍然关心,我确实在答案中将类型从 int 更改为 size_t
      【解决方案3】:
      printf("CipherText: );
      for (int i = 0; i < strlen(text); i++)
      {
      char c = plaintext[i];
      printf(" %c", c + 1);
      }
      

      【讨论】:

      • 那是输出.... CipherText: iCipherText: fCipherText: mCipherText: mCipherText:
      • 为什么是 2 个 printf?你可以做printf("CipherText: %c", c+1);
      • @Lucas 如果您在 1 个 printf 中执行,则“CipherText”将在每个循环中打印。
      【解决方案4】:

      你也可以用这种简短的方式来做:

      #include <stdio.h>
      
      int main(void) {
        char str[] = "hello";
        printf("PlainText: %s\nCipherText: ", str);
      
        for( int i = 0; str[i] != '\0'; 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

      【讨论】:

        【解决方案5】:

        您必须从 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");
        }
        

        【讨论】:

          猜你喜欢
          • 2018-10-17
          • 1970-01-01
          • 2022-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-27
          • 2022-06-11
          • 1970-01-01
          相关资源
          最近更新 更多