3
votes

I'm creating a program to reverse lines (each character) using 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. 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. How can I do that? I tried putting 1 bigger loop while (ch != EOF) outside while (ch != '\n') but that didn't work.

Thanks for helping in advance.

3
Outside just the while(ch != '\n') block or all the following code that reverses the line too? You need to post the code that doesn't work.Dave Rager
You actually need three conditions for your first loop: i < MAX_CH and ch != '\n' and ch != EOFwildplasser

3 Answers

1
votes

You want to repeat the code for as long as you can read lines of input.

Here a a few issues to address

  • the arrays do not need to have int type, char type is a more appropriate choice.
  • output a newline after the reversed line.
  • you do not need a separate array to store the reversed line, you can just print the characters in the reverse order from the string array.
  • you can use putchar instead of printf for simplicity

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;
}
0
votes

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);
0
votes

Here is a bit more universal version. Reads from the file or stdin, any line length supported (at least until realloc fails).

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");
}