{"content":{"id":620,"title":"K\u0026R Solutions - 1.19","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a function 'reverse(s)' that reverses the character string 's'. Use it to write a program that reverses the input one line at a time.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\n// Using the same basic format is previous exercises and examples\r\n#define MAXLINE 1000\r\n\r\nint get_a_line(char s[], int lim);\r\nvoid reverse(char s[]);\r\n\r\nint main(void)\r\n{\r\n char line[MAXLINE];\r\n\r\n while ((get_a_line(line, MAXLINE)) \u003e 0) {\r\n reverse(line);\r\n printf(\"%s\", line);\r\n }\r\n\r\n return 0;\r\n}\r\n\r\nint get_a_line(char s[], int lim)\r\n{\r\n int c, i;\r\n\r\n for (i = 0; i \u003c lim-1 \u0026\u0026 (c=getchar()) != EOF \u0026\u0026 c != '\\n'; ++i) {\r\n s[i] = c;\r\n }\r\n\r\n if (c == '\\n') {\r\n s[i] = c;\r\n ++i;\r\n }\r\n s[i] = '\\0';\r\n\r\n return i;\r\n}\r\n\r\nvoid reverse(char s[])\r\n{\r\n char temp[MAXLINE];\r\n int i, j;\r\n\r\n i = 0;\r\n while ((temp[i] = s[i]) != '\\0')\r\n ++i;\r\n\r\n /* Reversing the newline looks ugly. In real life, we might strip that \r\n * char of whitespace before reversing it, as per the last lesson. */\r\n j = 0;\r\n --i;\r\n s[i--] = '\\n';\r\n while (i \u003e= 0) {\r\n s[i] = temp[j];\r\n ++j;\r\n --i;\r\n }\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e./reverse \r\nhi\r\nih\r\nsananab is a good blog\r\ngolb doog a si bananas\r\ntacocat\r\ntacocat\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2019-04-29T00:00:00.000Z","created_at":"2019-03-30T17:08:48.000Z","updated_at":"2019-03-30T17:36:31.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

K&R Solutions - 1.19

Q: Write a function 'reverse(s)' that reverses the character string 's'. Use it to write a program that reverses the input one line at a time.

A:

#include <stdio.h>

// Using the same basic format is previous exercises and examples
#define MAXLINE 1000

int get_a_line(char s[], int lim);
void reverse(char s[]);

int main(void)
{
  char line[MAXLINE];

  while ((get_a_line(line, MAXLINE)) > 0) {
    reverse(line);
    printf("%s", line);
  }

  return 0;
}

int get_a_line(char s[], int lim)
{
  int c, i;

  for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i) {
    s[i] = c;
  }

  if (c == '\n') {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';

  return i;
}

void reverse(char s[])
{
  char temp[MAXLINE];
  int i, j;

  i = 0;
  while ((temp[i] = s[i]) != '\0')
    ++i;

  /* Reversing the newline looks ugly. In real life, we might strip that 
   * char of whitespace before reversing it, as per the last lesson. */
  j = 0;
  --i;
  s[i--] = '\n';
  while (i >= 0) {
    s[i] = temp[j];
    ++j;
    --i;
  }
}
./reverse 
hi
ih
sananab is a good blog
golb doog a si bananas
tacocat
tacocat