2

I have this program

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

int main()
{
    char text[30];
    int i,j,n;
    puts("Enter the Text:");
    gets(text);
    n=strlen(text);
    for(i=n;i>=0;i--)
    {
       if(text[i-1]==' '||text[i-1]==NULL )
       {
           for(j=i;text[j]!=' ';j++)
           {
               printf("%c",text[j]);
           }
       }

       printf(" ");


    }

    getche();
}

Suppose if i input is "I am Happy" then my output is "Happy am I"

I am not sure where i went wrong in this program, i am not getting all the words , I am getting result as "happy [=w am " .Please programmers help me.

Thanks In Advance.

i have found the answer , thanks for your helps, Below is my code

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

int main()
{
    char text[100];
    int i,j;
    puts("Enter the Text:");
    gets(text);
    strrev(text);
    for(i=0;text[i]!='\0';i++)
    {
       if(text[i+1]==' ' || text[i+1]==NULL)
       {
           for(j=i;j>=0 && text[j]!=' ';j--)
            printf("%c",text[j]);
       }
       printf(" ");
    }
    getche();
}
miensol
  • 39,733
  • 7
  • 116
  • 112
Mahi
  • 553
  • 2
  • 7
  • 23
  • 3
    This is a 'standard' interview question. And has been answered on SO before... – Mitch Wheat Aug 18 '13 at 09:17
  • Check the 'strtok' function .. iterate through, load array, then iterate through the array backwards.. might be a nice reverse strtok somewhere? – FreudianSlip Aug 18 '13 at 09:21
  • you have more than 5 duplicate questions about reverse stringe in C on SO – Ilan Aug 18 '13 at 09:21
  • 1
    "I am not sure where i went wrong" - you used `gets()` instead of `fgets()`... –  Aug 18 '13 at 09:21
  • 1
    @H2CO3 Fgets() is not the replacement, if gets() works.. Fine, but if you want to avoid gets(), instead you can also use gets_s() they have they're ups and downs... Just please don't rage on people for using gets.... It's good for that are starting to learn the basic concepts... Then later, they can maybe worry about buffer overflow attacks, etc.. Regards. – Joe DF Aug 18 '13 at 11:40
  • @JoeDF If one doesn't care about buffer sizes at the beginning, he won't care later either. And if that's the case, he should not be learning C but Python or JavaScript instead. (Oh, and `fgets()` **is the** replacement of `gets()`, which is **deprecated.**) –  Aug 19 '13 at 07:05
  • Oh so Scanf is crap also then? You might as well loop with `fgetc(stdin)` ... – Joe DF Aug 19 '13 at 18:17

4 Answers4

4

Basically md5's solution, implemented using recursion:

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

void print_upto_space(const char *s)
{
    do {
        putc(*s, stdout);
    } while (*s++ != ' ');
}

void reverse_print(const char *s)
{
    const char *p = strchr(s, ' ');
    if (p == NULL) { 
        printf("%s ", s);
    } else {
        reverse_print(p + 1);
        print_upto_space(s);
    }
}
  • 1
    I look like a culprit, since every answer had got a downvote except mine, but I've upvoted all of them. :p – md5 Aug 19 '13 at 17:37
2

You can use the following algorithm:

  1. Split the string into words in an array of strings (let's say A).
  2. Print A in reverse order.

Although this is O(n) in space, this is perhaps the easier algorithm (since your string is only 30 characters long, it doesn't matter).

md5
  • 23,373
  • 3
  • 44
  • 93
  • I have found the Answer #include #include int main() { char text[100]; int i,j; puts("Enter the Text:"); gets(text); strrev(text); for(i=0;text[i]!='\0';i++) { if(text[i+1]==' ' || text[i+1]==NULL) { for(j=i;j>=0 && text[j]!=' ';j--) printf("%c",text[j]); } printf(" "); } getche(); } Thanks – Mahi Aug 18 '13 at 11:30
  • @user2526830 That comment isn't useful at all. It's unreadable because it's not formatted properly. I suggest you post a proper answer if you wish to share your solution. –  Aug 19 '13 at 17:22
  • @H2CO3 : can u help me how to format this code to be usefull , i tried ctrl+k by copying the code in this comments but i was not able to do that , i know to format the code while asking the question , but i wasn't knowing in the comments section, please help me as i am new learner.Thanks In Advance. – Mahi Aug 19 '13 at 17:28
  • @user2526830 My point is, don't post your code as a comment. Post it as an answer. –  Aug 19 '13 at 17:31
  • @H2CO3 : How can i answser to my post , i tried to navigate through the page by scrolling from top to bottom , but i coudn't find an "answer" option , i can see only "Add Comment" option , please help me. – Mahi Aug 19 '13 at 17:34
  • @user2526830 Ah, I'm sorry. Indeed. Because your question has been closed, it's no longer possible to answer it. –  Aug 19 '13 at 17:35
1

You can try this to reverse the string:-

void reverseWords( char * str )
{
    int i = 0, j = 0;
    reverseString( str, strlen(str) ); 
    while( 1 ) // Loop forever
    {
        if( *(str+j) == ' ' || *(str+j) == '\0') 
        {
            reverseString( str+i, j-i );
            i = j+1;
        }
        if( *(str+j) == '\0')
        {
            break;
        }
        j++;
    }
} 
void reverseString(char* str, int len)
{
    int i, j;
    char temp;
    i=j=temp=0;

    j=len-1;
    for (i=0; i<j; i++, j--)
    {
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
    }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

This solution does not need any extra memory buffers.

Reverse first the whole string, using two pointers.

Then walk the string and reverse each word, using another two pointers.

Vorac
  • 8,726
  • 11
  • 58
  • 101