I am following Kernighan and Ritchie to learn C and my solution to exercise 1.19, to reverse a string, does not work. Intermediate print out indicates it is working, but the string is not reversed. Please help, and thanks.

#include <stdio.h>
#define MAXLINE   1000 // max allowed line length

int getline(char line[], int max);
void reverse(char line[], int len);

/* reverse the characters in a line of input */
main() {
    int len;   // current line length

    char line[MAXLINE];      // current line

    while((len = getline(line, MAXLINE)) > 0) {
        printf("before:***%s***\n", line);
        reverse(line, len);
        printf("after:***%s***\n", line);
    }

    return 0;
}

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

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

    s[i] = '\0';
    return i;
}

void reverse(char line[], int len) {
    int i, m = 0, x = 0;
    char c;

    printf("reverse before:\n");
    for(x = 0;x < len;x++)
        printf("%d ", line[x]);
    printf("\n");

    for(i = len - 1;i >= 0;i--, m++) {
        c = line[m];
        printf("i: %d\n", i);
        printf("c: %d\n", c);
        printf("line[m] BEFORE: %d\n", line[m]);
        printf("line[i] BEFORE: %d\n", line[i]);
        line[m] = line[i];
        line[i] = c;
        printf("line[m] AFTER: %d\n", line[m]);
        printf("line[i] AFTER: %d\n", line[i]);
    }
    printf("reverse after:\n");
    for(x = 0;x < len;x++)
        printf("%d ", line[x]);
    printf("\n");
}

Here is the fix:

for (i = len - 1; i > m; i--, m++) {

I'll leave it to you to figure out why that fixes it, but working through the logic with a small line of text on a piece of paper will help greatly. :)

I think you are doing a good job. Continue.
I recomend you to change the reverse function with:

void reverse(char line[], int len) {
  int i;
  char tmp;
  --len;
  for(i=0; i<=len/2; i++)
    {
      tmp = line[i];
      line[i] = line[len-i];
      line[len-i]=tmp;
    }
}

To understand it, you must work with a small line of text on a piece of paper as deceptikon said.

#include <stdio.h>
#include <string.h>

char *reverseme(char *str)
{
    int len = strlen(str);
    char temp;
    int i;
    len--;
    for (i = 0; i < len/2; i++)
    {
        temp = str[i];
        str[i] = str[len-i];
        str[len-i] = temp;
    }
    return;
}

main()
{
    char line[100];
    printf("Enter a line: ");
    gets(line);
    reverseme(line);
    puts(line);
    getchar();
}
#include <stdio.h>
#include <string.h>

main()
{
    char str[50];
    printf("Enter a string: ");
    gets(str);
    char* token = strtok(str, " ");
    int i;
    while (token != NULL)
    {
        size_t len = strlen(token);
        len--;
        for (i = len; i >= 0; i--)
        {
            putchar(token[i]);
        }
        putchar(' ');
        token = strtok(NULL, " ");
    }
    getchar();
    return 0;
}

Pakistan Zindabad = natsikaP dabadniZ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.