您能否解释一下为什么数组会“反转”,特别是在我粗体文本的地方?以下是打印的内容(输入“hi There”):“THE LINE (hi There) IN REVERSE:”。
此外,for 循环中的 print 语句在最终迭代中将其打印为“反转数组:?ereht ih-”。
谢谢!
int reverseit(int length, char line[]); // Function declaration
#define MAXLENGTH 1000
int main()
{
int length = 0;
char line[MAXLENGTH]; // array declaration
gettheline(length, line);
...
}
void gettheline(int length, char line[])
{
int i, c = 0;
while((c=getchar())!= (EOF))
{
if (c!='\n') // we'll put the newline character in place ourselves when print in reverse is complete
{
line[i] = c; // until you see \n, keep setting each char to line[i]
printf("line[%d]=%c\n", i, line[i]);
i++; // increment i
length = i;
}
else if(i>0) // else if c is == \n and i>0 , reverse it, and put \n at the end
{
// store reverse of the line and stick \n at the end
reverseit(length, line);
}
}
int reverseit(int length, char line[]) // pass in "length" from getline
{
int i, j = 0;
char a;
char reversed[length];
for (i=length;i>-1;i--) //count down from length to 0
{
reversed[i] = line[j++];
printf("line[%d] = reversed[%d] = %c\n", i, j, line[i]);
printf("reversed array: %s", reversed);
}
**printf("THE LINE (%s) IN REVERSE: %s", line, reversed);**
...
}
最佳答案
您可能想得太多了,您可能想从 main
调用字符串反转,而不是在 getttheline
中调用以生成原始的 line
可返回到 main
中,而不是作为 getttheline
中的 transient 输入位。这取决于你。
您的 getttheline
函数实际上只需要从 stdin< 读取一行(以
。您已经传递了'\n'
或 EOF
终止)/length
,为什么不传递地址length
(例如&length
,并设置参数 int *
) 以使字符串的最终长度在调用函数中可用(此处为 main
)。例如,从 stdin
读取一行输入所需的就是:
void gettheline(int *length, char *line)
{
int c = 0;
*length = 0;
while (*length + 1 < MAXLENGTH && (c=getchar()) != '\n' && c != EOF)
line[(*length)++] = c;
line[*length] = 0; /* nul-terminate */
}
上面您使用存储在 length
指向的地址处的值作为计数器来验证您在 行中存储的字符数不超过
(为 nul 终止 字符保留空间)。您只需使用 MAXLENGTH - 1
个字符gethar()
读取每个字符,直到遇到 '\n'
或 EOF
,此时结束读取循环并肯定地在length
个字符处nul-terminate(因为您将line
初始化为所有零
,所以它已经完成了,但是始终肯定地终止您的字符串是一种很好的做法。
至于你的反转,你所需要的只是一个指向 line
的 start 和 end 指针(你拥有它length
),那么您可以通过交换从两端开始并向中间移动的字符,非常有效地就地反转字符串——例如每次迭代交换 2 个字符,
/** strreverse - reverse string given by begin and end pointers.
* Takes valid string and swaps src & dest each iteration.
* The original string is not preserved.
* If str is not valid, no action taken.
*/
void strreverse (char *begin, char *end)
{
char tmp;
if (!begin || !end) { /* validate both begin and end pointers */
printf ("error: invalid begin or end pointer\n");
return;
}
while (end > begin)
{
tmp = *end;
*end-- = *begin;
*begin++ = tmp;
}
}
将所有部分放在一个简短的 main
中,输出 line
及其反转,您可以执行如下操作:
#include <stdio.h>
void gettheline (int *length, char *line);
void strreverse (char *begin, char *end);
#define MAXLENGTH 1000
int main (void)
{
int length = 0;
char line[MAXLENGTH] = ""; /* declare & initialize to zeros */
gettheline (&length, line);
if (length) {
printf ("line: %s (%d chars)\n", line, length);
strreverse (line, line + length -1);
printf (" rev: %s\n", line);
}
else
fprintf (stderr, "error: line length is zero.\n");
return 0;
}
示例使用/输出
$ echo "my dog has fleas" | ./bin/strrevgetchar
line: my dog has fleas (16 chars)
rev: saelf sah god ym
仔细检查一下,如果您还有任何其他问题,请告诉我。
关于c - printf ("%s",数组);不打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336030/