-2

I have been trying to reverse the order of a sentence containing a string using pointers. My code snippet:

int main()
{
char c[50];
char *ptr;
int i=0,len;
gets(c);
ptr=c;
len=strlen(c);
for(i=len-1;i>=0;i--)
  printf("%c",*(ptr+i));
return 0;
}

This code generates a reverse of the string I enter correctly. Like for example:
Input: how are you
output: uoy era woh

But what I kept in mind was entirely different, I just wanted the order of the words in the sentence to reverse and not the exact words to reverse. For example, for the same input "how are you" I want the output to be "you are how". Ive been breaking my head for quite some time now. Is there something that Im missing???

Re-EDIT: I want to do the reverse concept using pointers and pointers only!!! thats why i posted as a new question!!!

As suggested by another user, I tried using the strtok() function too and my output works kinda fine but is throwing garbage values.... Kindly help to this context::

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

int main()
{
char c[50],d[50];
char *ptr,*aptr,*tok;
int i=0,j=0,len;
gets(c);
ptr=c;
aptr=d;
len=strlen(c);
printf("%d\n",len);
for(i=len-1;i>=0;i--,j++)
  *(aptr+j)=(*(ptr+i));
for(i=0;i<len;i++)
 printf("%c",d[i]);
printf("\n");
tok=strtok(aptr," ");
while(tok!=NULL)
{
 printf("%s\n",strrev(tok));
 tok=strtok(NULL,aptr);
}
return 0;
}

and i Finally landed at the most optimized answer!!!!

#include <stdlib.h>

int main()
{
char s[20][20];
int i=0,length=-1;
for(i=0;;i++)
{
    scanf("%s",s[i]);
    length++;
    if(getchar()=='\n')
        break;
}
for(i=length;i>=0;i--)
    printf("%s ",s[i]);
return 0;
}
Sriram Jayaraman
  • 800
  • 1
  • 8
  • 15
  • 1
    Yes you reverse character of each word instead of each complete word. That is the problem. – ameyCU Aug 03 '16 at 15:42
  • Split the string by spaces and store each word in a string, then reverse the order of the strings. – flau Aug 03 '16 at 15:43
  • I'm not familiar with C but I don't see any code for splitting it into words. – gcampbell Aug 03 '16 at 15:43
  • but i cant seem to work them as a single word each – Sriram Jayaraman Aug 03 '16 at 15:43
  • 1
    [strtok()](http://man7.org/linux/man-pages/man3/strtok.3.html) could be a starting point – Quentin Aug 03 '16 at 15:45
  • 2
    Possible duplicate of [how to perform reversing a sentence Word by Word in C?](http://stackoverflow.com/questions/9630673/how-to-perform-reversing-a-sentence-word-by-word-in-c) or [reversing words in a sentence](http://stackoverflow.com/questions/3276582/reversing-words-in-a-sentence) – smac89 Aug 03 '16 at 15:47
  • Handle the reversing in a different function. Call that function on the whole string, and then call it on each word. – Awais Chishti Aug 03 '16 at 15:47
  • i just tried strtok it seems cool!! but just that i am able to separate the words but end up not being able to store them in another string – Sriram Jayaraman Aug 03 '16 at 15:52
  • I wanted to use the concept of pointers – Sriram Jayaraman Aug 03 '16 at 16:46

1 Answers1

0

Print string when you just after a spacebar or at the beginning:

for(i=len-1;i>=0;i--)
  if (i==0) || (*(ptr+i-1)==' ') { 
    printf("%s ",*(ptr+i));
    if (i!=0)
      *(ptr+i-1)='\0'; // Cutting the next print. Attention: that changes the original string.
  }
return 0;
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31