HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Reverse a string without the <string.h> header

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withouttheheaderreversestring

Problem

I'm learning C from the K.N. King book and just arrived at a question to reverse the words in a sentence.

Here is the output:


Enter a sentence: you can cage a swallow can't you?


Reversal of sentence: you can't swallow a cage can you?

Though I've done this and it runs correctly, I want to make my code shorter. I'm not asking for any other solution.

#include 

#define N 100

int main(void) {
  char ch[N], terminate;
  int i, j, k, len;
  printf("Enter a sentence: ");
  for(i = 0; i = 0; j--) {
    word_count++;
    if(ch[j] == ' ') {
      for(k = j+1; k < len; k++) {
        printf("%c", ch[k]);
      }
      printf("%c", ch[j]);  // print space character
      len = len - word_count;
      word_count = 0;
    }

  }

  for(i = 0; i < len; i++) {
    printf("%c", ch[i]);
    if(ch[i] == ' ') {
      break;
    }
  }
  printf("%c", terminate);

  return 0;
}


This can be done only with loops, arrays, getchar() and putchar() methoda using only ` header and no extra header (restrict header functions and methods). Also, no puts() and gets()` functions. I guess this can be done only with two loops.

Solution

I have a few comments.

-
main takes argc/argv normally. Also start the function with the opening
brace in colum 0

-
Define one variable per line.

-
Your request for input might be better printed to stderr so that it can be
separated from program output.

-
Your initial loop should test for EOF. Currently it doesn't You can test
this in the *NIX shell with (program is your executable):

/bin/echo -n "hello world" | ./program


-
terminate seems redundant

-
You can and perhaps should define loop variables in the loop (as long as
you don't need the value outside the loop):

for (int i = 0; i < n; ++i) {


-
The second for-loop is a little messy. For a start it has a nested loop -
my advice is to avoid nested loops and extract the inner loop to a function.
This is not always practical, but more often than not it is useful. In this
case, you can write a little function (eg. print_chars) that prints a
specified number of characters.

-
Your terminating loop can be replaced by a call to the print_chars
function.

-
putchar is preferable to printf("%c", ...)

-
while, for, if etc should be followed by a space.

-
Testing for ' ' might be better done using isspace, which will test for
other space-like characters too.

Here is my version, for what it is worth:

#include 
#include 

#define N 100

static inline void print_chars(const char *s, long n)
{
    for (int i = 0; i = N-1) {
            break;
        }
        line[len++] = (char) ch;
    }
    line[len] = '\0';

    char *end = line + len;
    char *s = end;
    while (--s >= line) {
        if (isspace(*s)) {
            print_chars(s + 1, end - s - 1);
            putchar(*s);
            end = s;
        }
    }
    print_chars(line, end - line);
    if (ch != EOF) {
        putchar(ch);
    }
    return 0;
}

Code Snippets

/bin/echo -n "hello world" | ./program
for (int i = 0; i < n; ++i) {
#include <stdio.h>
#include <ctype.h>

#define N 100

static inline void print_chars(const char *s, long n)
{
    for (int i = 0; i < n; ++i) {
        putchar(s[i]);
    }
}

int main(int argc, char **argv)
{
    char line[N];
    int ch; /* note that ch is an int to allow it to hold 'EOF' */
    int len = 0;

    fprintf(stderr, "Enter a sentence: ");

    while ((ch = getchar()) != EOF) {
        if (ch == '?' || ch == '.' || ch == '!' || ch == '\n' || len >= N-1) {
            break;
        }
        line[len++] = (char) ch;
    }
    line[len] = '\0';

    char *end = line + len;
    char *s = end;
    while (--s >= line) {
        if (isspace(*s)) {
            print_chars(s + 1, end - s - 1);
            putchar(*s);
            end = s;
        }
    }
    print_chars(line, end - line);
    if (ch != EOF) {
        putchar(ch);
    }
    return 0;
}

Context

StackExchange Code Review Q#22914, answer score: 4

Revisions (0)

No revisions yet.