[英]Reversing characters until EOF
I'm creating a program to reverse lines (each character) using getchar
. 我正在创建一个程序来使用
getchar
来反转行(每个字符)。 This is what I've got so far (my code is a bit messy since I'm a beginner). 这是我到目前为止所得到的(因为我是初学者,我的代码有点乱)。
#include <stdio.h>
#define MAX_CH 256
int main(void)
{
int ch = 0;
int i = 0;
int string[MAX_CH] = {0};
while (ch != '\n')
{
ch = getchar();
string[i] = ch;
++i;
}
i = i - 2; // put i back to the position of the last character
int limit = i;
int n;
int reverse[MAX_CH] = {0};
for (n = 0; n <= limit; ++n)
{
reverse[n] = string[i];
--i;
}
for (n = 0; n <= limit; ++n)
{
printf("%c", reverse[n]);
}
return 0;
}
This code however only works for 1 line. 但此代码仅适用于1行。 I want to upgrade it to be able to read and print the reverse text immediately after and work for multiple lines until it reaches EOF.
我想升级它,以便能够立即读取和打印反向文本,并为多行工作,直到它达到EOF。 How can I do that?
我怎样才能做到这一点? I tried putting 1 bigger loop
while (ch != EOF)
outside while (ch != '\\n')
but that didn't work. 我试图把1个大循环
while (ch != EOF)
之外while (ch != '\\n')
但没有奏效。
Thanks for helping in advance. 感谢您提前帮助。
You want to repeat the code for as long as you can read lines of input. 只要您可以读取输入行,您就想重复代码。
Here aa few issues to address 这里有一些问题需要解决
int
type, char
type is a more appropriate choice. int
类型, char
类型是更合适的选择。 string
array. string
数组中以相反的顺序打印字符。 putchar
instead of printf
for simplicity putchar
而不是printf
Here is a modified version: 这是一个修改版本:
#include <stdio.h>
#define MAX_CH 256
int main(void) {
int ch, i, length;
char string[MAX_CH];
for (;;) {
for (i = 0; i < MAX_CH; i++) {
if ((ch = getchar()) == EOF || (ch == '\n'))
break;
string[i] = ch;
}
length = i;
if (length == 0) {
/* no character read => EOF */
break;
}
for (i = 0; i < length; i++) {
putchar(string[length - i - 1]);
}
putchar('\n');
}
return 0;
}
You can use a loop like: 您可以使用如下循环:
do {
i=0;
while ((ch=getchar())!=EOF && ch!='\n') {
string[i++] = ch;
}
string[i] = '\0'; // terminate it
for (int j=0, i--; j<i; j++, i--) { // reverse line
char tmp= string[j];
string[j]= string[i];
string[i]= tmp;
}
} while (ch != EOF);
Here is a bit more universal version. 这是一个更普遍的版本。 Reads from the file or stdin, any line length supported (at least until realloc fails).
从文件或stdin读取,支持任何行长度(至少在realloc失败之前)。
char *reverse(char *str)
{
size_t len = strlen(str);
for (size_t pos = 0; pos < len / 2; pos++)
{
char tmp = str[pos];
str[pos] = str[len - 1 - pos];
str[len - 1 - pos] = tmp;
}
return str;
}
#define MEMINC 80
int PrintReversedLines(char *filename)
{
FILE *fi = !strcmp(filename, "stdin") ? stdin : fopen(filename, "rt");
char *tmpbuff = NULL, *buff = NULL;
size_t pos = 0;
int ch = 0;
if (!fi) return -1;
while ((ch = fgetc(fi)) != EOF)
{
if (!(pos % MEMINC))
{
if (!(tmpbuff = realloc(buff, pos + 80)))
{
if (buff) free(buff);
return -1;
}
else
{
buff = tmpbuff;
}
}
if (ch == '\r') continue;
if (ch == '\n')
{
buff[pos++] = 0;
printf("%s\n", reverse(buff));
free(buff);
buff = NULL;
pos = 0;
}
else
{
buff[pos++] = ch;
}
}
return 0;
}
int main()
{
PrintReversedLines("stdin");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.