【问题标题】:printing strings populated with a loop in C?在C中打印填充有循环的字符串?
【发布时间】:2017-09-09 14:14:37
【问题描述】:

我是 C 新手,正在尝试学习打印字符串和数组的单个元素。

在主函数中,我创建了一个包含 5 个字符的 char 数组,并且能够打印字符串。我还可以打印数组的 1 个元素。这一切都很好。

然后我创建了一个函数,它有一个 for 循环来填充一个具有 26 个可用插槽的数组,并且想要做同样的事情,打印整个字符串,然后只打印它的单个元素。

当我的程序执行时,它应该打印: 你好 ○ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ (这应该打印两次,一次用于 for 循环,然后一次当我尝试打印字符串时) C

但是,当我这次尝试打印字符时,我遇到了错误,我能看到的唯一区别是我初始化数组的方式。我不明白其中的区别以及它为什么会破裂。它与 for 循环的范围有关吗?我也尝试将 char 数组设为静态,但这也不起作用。 有人可以帮忙吗?

这是我的代码

我是 C 新手,正在尝试学习打印字符串和数组的单个元素。

在主函数中,我创建了一个包含 5 个字符的 char 数组,并且能够打印字符串。我还可以打印数组的 1 个元素。这一切都很好。

然后我创建了一个函数,它有一个 for 循环来填充一个具有 26 个可用插槽的数组,并且想要做同样的事情,打印整个字符串,然后只打印它的单个元素。

当我的程序执行时,它应该打印: 你好 ○ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ (这应该打印两次,一次用于 for 循环,然后一次当我尝试打印字符串时) C

但是,当我这次尝试打印字符时,我遇到了错误,我能看到的唯一区别是我初始化数组的方式。我不明白其中的区别以及它为什么会破裂。它与 for 循环的范围有关吗?我也尝试将 char 数组设为静态,但这也不起作用。 有人可以帮忙吗?

void mystring()
{

    char s[26];
    for(char ch = 'A'; ch < 'Z'; ch++)
    {
        int i = 0;
        s[i] = ch;
        printf("%c", s[i]);
        i++;
    }

    printf("\n%s\n", s);
    printf("%c", s[2]);

}

int main()
{
    char mystr[5] = "Hello";
    printf("%s\n", mystr);
    printf("%c\n", mystr[4]);

    mystring();
}

【问题讨论】:

  • 你遇到了什么错误?
  • int i = 0; 这个范围可能是错误的

标签: c arrays loops


【解决方案1】:
  1. 你的两个“strings”都没有null终结符,我写的是“strings”,因为根据定义它们实际上不是字符串 中的 字符串

  2. 您的i 变量始终为0,只需使用简单的公式ch - 'A' 即可给出正确的索引。

  3. 您的char mystr[5] = "hello"; 空间不足,您也需要为终止的'\0' 分配空间。

要修复您的代码,您可以这样做

char s[27];
for (int ch = 'A'; ch <= 'Z'; ++ch) {
    s[ch - 'A'] = ch;
}
s[26] = '\0';

和,

char mystr[6] = "hello";

【讨论】:

    【解决方案2】:

    首先,您应该在for 循环之外声明i 值,否则i++ 命令无效,因为i 将始终为0

    在 C 中 strings 是带有 char 元素的数组,因此在这些容器的末尾您应该始终放置 null 字符。因此,为了具体起见,您必须将数组的大小增加1(27 而不是 26)并在字符串的最终位置,当您完成创建它时,s[26] = '\0';

    【讨论】:

      【解决方案3】:

      目前,我看到的主要错误是

      for(char ch = 'A'; ch < 'Z'; ch++)
          {
              int i = 0;
              s[i] = ch;
              printf("%c", s[i]);
              i++;
          }
      

      您想将声明和初始化语句 (int i = 0) 移到循环之外。现在,它每次都将 i 重置为 0(实际上,可能会给您错误,因为它试图声明一个具有相同名称的变量)

      【讨论】:

        【解决方案4】:

        我要评论你的代码。希望对您有所帮助。

        void mystring()
        {
        
            char s[26];//you need one extra space for the null terminating char, so it should be "char s[27]"
            for(char ch = 'A'; ch < 'Z'; ch++)//if you want to print 'Z' as well, this should be "ch <= 'Z'", otherwise it stops at 'Y', which is the last char less than 'Z'
            {
                int i = 0;//you should declare this i before the loop, along with your char array. The way you're doing it right now, on every loop iteration you redeclare i and give it the value 0, meaning you're just filling the first position of your char array over and over
                s[i] = ch;
                printf("%c", s[i]);
                i++;
            }
            //s[26] = '\0'; //you should add this line <--remember, strings in C are null-terminated
            printf("\n%s\n", s);
            printf("%c", s[2]);
        
        }
        
        int main()
        {
            char mystr[5] = "Hello";//strings in C are null terminated so this should have an extra space "char mystr[6]"
            printf("%s\n", mystr);
            printf("%c\n", mystr[4]);
        
            mystring();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-07
          • 1970-01-01
          • 1970-01-01
          • 2022-12-10
          • 2021-01-28
          • 1970-01-01
          • 1970-01-01
          • 2014-02-17
          相关资源
          最近更新 更多