hi all
Please help me. I am A bgineer of C language and i want a solution of my question that is;--- "A program that print each world of a given string reverse".
If any one of you can solve this than please give me an answer

Member Avatar for iamthwee
#include <stdio.h>
#include <conio.h>
void main( void )
  char a[100];
  char b[10][20];
  int x, m = 0, p, wrd = 0, i = 0;
  printf ( "Enter a sentence:" );
  gets ( a );
  for ( x = 0; a[x] != '\0'; x++ )
     if ( a[x] == ' ' )
        for ( p = m; p <= x; p++ )
           b[wrd][i++] = a[p];
           b[wrd][i] = '\0';
           m = x + 1;
           i = 0;
  i = 0;
  if ( a[x] == '\0' )
     for ( p = m; p <= x; p++ )
        b[wrd][i++] = a[p];
        b[wrd][i] = '\0';
  for ( i = wrd; i >= 0 ; i-- )
     int j = 0;
     while ( b[i][j] != '\0' )
        printf ( "%c", b[i][j++] );
     printf ( "\n" );
commented: A spoonfed answer, and a terrible one at that - void main, gets(), unportable. -2
commented: I see sarcasm. Salem didn't or he didn't like it. +6

You'd think some people would know by now not spoon-feed complete answers without any demonstration of effort from the OP

> void main( void )
This is simply wrong. main returns an int - no ifs buts or maybes.

> gets ( a );
This is utterly dangerous. Use fgets() to read a line safely.

> #include <conio.h>
This is gratuitous unportability for no good reason.

commented: unnecessary colouring parts of your post in green, and most notably against the forum rules :) -2
commented: The OP is the beneficiary of the nice explanation. +6

I would suggest to tokenize the string first.

Of what use would tokenizing the string be?
The question isn't to reverse the words in a string.

This looks suspiciously like a question intended by a job interviewer to filter out the people who can't code at all.

>The question isn't to reverse the words in a string.
Yes, it is. "Print each word of a given string in reverse". There's just no requirement that the reversal be permanent, which gives one quite a few options in solving the problem. Tokenizing the string is one of them. In order of impressiveness (from least to most) without adding any new restrictions, I would say these are the expected solutions:

1) iamthwee's copy to extra storage (which is technically tokenizing ;))
2) A direct print using a similar technique as iamthwee
3) Tokenizing the string and printing it with extra storage
4) Tokenizing the string and printing it with recursion
5) A recursive print without tokenizing
6) A flip-flop in place reversal

If the original question is the start of an interview problem, I have no doubt that further restrictions would be added so as to direct the interviewee to one of the latter solutions.

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.