-6

I am trying to reverse the words of the string (not the words itself). I am trying to dynamically allocate 2 dimensional array only using pointers(not using array syntax). My program compiles fine but end up displaying only one word as output with spaces here and there. Here is my code.

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

void rev(char *x, int n);
int words(char * p);

int main()
{
    char *p = (char *)malloc(101*sizeof(char));
    fgets(p, 101, stdin);
    rev(p, words(p));
    free(p);
    return 0;
}

void rev(char *x, int n)
{
    char **y = (char **)malloc(101*sizeof(char *));
    int i = 0;

    int *j = (int *)calloc(101, sizeof(int));
    *y = (char *)malloc(101*sizeof(char));
    while(*x!='\n')
    {
        if(*x==' ')
        {
            i++;
            *(y+i) = (char *)malloc(101*sizeof(char));
        }
        else
        {

            *(*(y+i)+(*(j+i))) = *x;
            (*(j+i))++;
        }
        x++;
    }
    i++;
    int z, q;
    for(z=i-1; z>0; z--)
    {
        for(q=0; q<(*(j+i)); q++)
        {
            printf("%c", *(*(y+z)+(q)));
        }
        printf(" ");
    }
    for(q=0; q<*j; q++)
    {
        printf("%c", *(*(y)+(q)));
    }
    printf("\n");
    for(q=0; q<i; q++)
    {
        free(y+i);
    }
    free(j);
    free(y);
}

int words(char * p)
{
    int x = 0;
    while(*p!='\n')
    {
        if(*p==' ')
        {
            x++;
        }
        p++;
    }
    return ++x;
}

I want to do it only using pointer arithmetic. I have been trying it for past 20 hours and couldn't figure it out. Can anybody explain the mistake and how to correct it? Input Hello World Output

 Hello

Input This is my input Output

    This
bharadwaj
  • 492
  • 1
  • 4
  • 13
  • 1
    Note: They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Apr 14 '16 at 13:00
  • 1
    There is no 2D array in your code. `char **` is **not** a 2D array and cannot point to one. A pointer is not an array or vice-versa. And don't use _magic numbers_. – too honest for this site Apr 14 '16 at 13:01
  • `for(q=0; q – MikeCAT Apr 14 '16 at 13:01
  • @MikeCAT Is it due to this mistake, I am not getting required output ? – bharadwaj Apr 14 '16 at 13:02
  • Do you have working code using arrays? – MikeCAT Apr 14 '16 at 13:02
  • It is good to show us some examples of input and desired output. – MikeCAT Apr 14 '16 at 13:03
  • [Your program crashed](http://melpon.org/wandbox/permlink/jYyuNgjwfZCeAeOX). – MikeCAT Apr 14 '16 at 13:04
  • "end up displaying only one word as output with spaces here and there" [Couldn't reproduce](http://melpon.org/wandbox/permlink/zX3YVbTQKUCeC1hY) after removing the problematic `free`s. Some undefined behavior...? – MikeCAT Apr 14 '16 at 13:07
  • @MikeCAT I added input and output. I am using codeblocks. It is compiling and not showing any warnings in my IDE – bharadwaj Apr 14 '16 at 13:09
  • Please show your *desired* output. Posting actual output is somewhat helpful, but we can get them by executing the code. Also I advice that first you should use array to build working program, then convert it to pointer aritimetic using `E1[E2]` = `*((E1)+(E2))` rule. – MikeCAT Apr 14 '16 at 13:16

1 Answers1

1

This is a really tricky problem (Reversing the words in a string). One of the best solutions for this problem is that you define a function to reverse the whole string, then start reading word by word of the resulting string and if you arrive to an space, again use the reverse function and print it.

Original String

hello world

First Step

dlrow olleh

Second Step

world hello

This solution is much easier than your complicated solution.

This can be a possible reverse function which I mentioned above:

void rev(char *word, int len)
{
  char c;
  if(len>1)
  {
    c=word[len-1];
    word[len-1]=word[0];
    word[0]=c;
    rev(word+1, len-2);
  }
  return;
}
ReshaD
  • 936
  • 2
  • 18
  • 30