有人可以向我解释一下这个 C 程序如何反转字符数组吗?
Can somebody explain to me that how does this C program reverse an characters array?
下面写了一个程序。我不明白它是如何反转 char 数组的。我的意思是它工作正常,它确实反转了用户存储在 char 数组中的字符串,但我想知道它是如何工作的以及它如何反转顺序?基本上我不理解第一个 for 循环,它在主体中没有任何语句,而且缺少 for 循环的第一部分。请用简单易懂的词来解释,不要用典型或难懂的词。我不是以英语为母语的人。非常感谢。
#include <iostream>
using namespace std;
int main()
{
char name[99];
int counter=0;
cin >> name;
for(;name[counter]!='[=10=]'; counter++)
{}
cout << "\nName: ";
for (;counter > 0; counter--)
{
cout << name[counter-1];
}
}
C 字符串以空 ('\0') 结尾。第一个循环递增 counter
直到找到包含在 name
中的字符串的末尾。这由空字符指示。重要的部分是 counter
是在第一个 for
循环之外声明的,并且在执行第二个循环时它保持在范围内,并且具有相同的值。然后第二个循环从 name
中的字符串末尾开始打印字符,直到它打印第一个字符。
你的计数器从 0 开始。
for(;name[counter]!='[=10=]'; counter++){}
上面的 for 循环递增计数器直到它达到 '\0'。此时您的计数器已经递增到您键入的名称的字符数。例如:如果您输入 hello,计数器现在是 5。
for (;counter > 0; counter--){cout << name[counter-1];
}
在上面的 for 循环中,您首先将计数器的值为 5,然后反向打印出数组的每个字符,因为第一次迭代您的计数器为 5,您打印 name[4] o 然后计数器递减所以你打印 name[3]=l,然后 name[2]=l,
然后是 name[1]=e ,然后是 name[0] =h。注意:如果您的计数器的值为 n,则您正在打印 n-1。所以当你的计数器递减到 1 时,你打印 name[0]。然后计数器最终递减到 0,循环变为假。此外,您并没有反转数组元素本身,而只是反向打印它们。
在您的程序中,第一个 For 循环是要知道 name 变量中的字符数。
让我来解释一下它是如何工作的。
For循环的基本结构如
对于(i=10;i>0;i--)
{
//for
的正文部分
}
第一部分i=0是初始化,第二部分是条件,第三部分是Increment/Decrements。
在你的程序中,我们已经将计数器的值初始化为0。
这个循环只是为了计算字符数,所以在每个循环之后计数变量都会递增。我们不需要在正文部分写任何东西。
在完成第一个 for 循环后,count 变量的值与 name 变量的字符相同。
同样在第二个循环中,我们不需要初始化计数值,因为计数已经存储了一些值。
希望你现在明白了..!!
#include <iostream>
using namespace std;
int main()
{
char name[99];
int counter=0; // variable to store the number of characters in name.
cin >> name;
// the for loop is counting each letter until the end of the string, storing the result in counter.
for(;name[counter]!='[=10=]'; counter++)
{}
cout << "\nName: ";
// if the name you entered was "Billy" counter would = 5
for (;counter > 0; counter--)
{
// since counter = 5 counter subtracts 1 to get to the "5th" spot in the array which is when counter = 4
// name[0] = B
// name[1] = i
// name[2] = l
// name[3] = l
// name[4] = y
// now starting from position 4 in the array counter-- subtracts 1
// from counter each time it runs through the loop to get to each previous letter.
cout << name[counter-1];
}
}
下面写了一个程序。我不明白它是如何反转 char 数组的。我的意思是它工作正常,它确实反转了用户存储在 char 数组中的字符串,但我想知道它是如何工作的以及它如何反转顺序?基本上我不理解第一个 for 循环,它在主体中没有任何语句,而且缺少 for 循环的第一部分。请用简单易懂的词来解释,不要用典型或难懂的词。我不是以英语为母语的人。非常感谢。
#include <iostream>
using namespace std;
int main()
{
char name[99];
int counter=0;
cin >> name;
for(;name[counter]!='[=10=]'; counter++)
{}
cout << "\nName: ";
for (;counter > 0; counter--)
{
cout << name[counter-1];
}
}
C 字符串以空 ('\0') 结尾。第一个循环递增 counter
直到找到包含在 name
中的字符串的末尾。这由空字符指示。重要的部分是 counter
是在第一个 for
循环之外声明的,并且在执行第二个循环时它保持在范围内,并且具有相同的值。然后第二个循环从 name
中的字符串末尾开始打印字符,直到它打印第一个字符。
你的计数器从 0 开始。
for(;name[counter]!='[=10=]'; counter++){}
上面的 for 循环递增计数器直到它达到 '\0'。此时您的计数器已经递增到您键入的名称的字符数。例如:如果您输入 hello,计数器现在是 5。
for (;counter > 0; counter--){cout << name[counter-1];
}
在上面的 for 循环中,您首先将计数器的值为 5,然后反向打印出数组的每个字符,因为第一次迭代您的计数器为 5,您打印 name[4] o 然后计数器递减所以你打印 name[3]=l,然后 name[2]=l, 然后是 name[1]=e ,然后是 name[0] =h。注意:如果您的计数器的值为 n,则您正在打印 n-1。所以当你的计数器递减到 1 时,你打印 name[0]。然后计数器最终递减到 0,循环变为假。此外,您并没有反转数组元素本身,而只是反向打印它们。
在您的程序中,第一个 For 循环是要知道 name 变量中的字符数。
让我来解释一下它是如何工作的。
For循环的基本结构如
对于(i=10;i>0;i--)
{
//for
的正文部分
}
第一部分i=0是初始化,第二部分是条件,第三部分是Increment/Decrements。
在你的程序中,我们已经将计数器的值初始化为0。
这个循环只是为了计算字符数,所以在每个循环之后计数变量都会递增。我们不需要在正文部分写任何东西。
在完成第一个 for 循环后,count 变量的值与 name 变量的字符相同。
同样在第二个循环中,我们不需要初始化计数值,因为计数已经存储了一些值。
希望你现在明白了..!!
#include <iostream>
using namespace std;
int main()
{
char name[99];
int counter=0; // variable to store the number of characters in name.
cin >> name;
// the for loop is counting each letter until the end of the string, storing the result in counter.
for(;name[counter]!='[=10=]'; counter++)
{}
cout << "\nName: ";
// if the name you entered was "Billy" counter would = 5
for (;counter > 0; counter--)
{
// since counter = 5 counter subtracts 1 to get to the "5th" spot in the array which is when counter = 4
// name[0] = B
// name[1] = i
// name[2] = l
// name[3] = l
// name[4] = y
// now starting from position 4 in the array counter-- subtracts 1
// from counter each time it runs through the loop to get to each previous letter.
cout << name[counter-1];
}
}