Reverse print

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 1
Reputation: Anoop Dogra is an unknown quantity at this point 
Solved Threads: 0
Anoop Dogra Anoop Dogra is offline Offline
Newbie Poster

Reverse print

 
0
  #1
Jul 13th, 2007
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
Last edited by Anoop Dogra; Jul 13th, 2007 at 3:49 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reverse print

 
0
  #2
Jul 13th, 2007
#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;
           wrd++;
           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" );
  }
  getch();
}
Last edited by iamthwee; Jul 13th, 2007 at 4:47 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reverse print

 
0
  #3
Jul 13th, 2007
You'd think some people would know by now not spoon-feed complete answers without any demonstration of effort from the OP
http://www.daniweb.com/forums/announcement118-2.html

> 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.

Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: Reverse print

 
0
  #4
Jul 13th, 2007
I would suggest to tokenize the string first.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: azimuth0 is an unknown quantity at this point 
Solved Threads: 5
azimuth0 azimuth0 is offline Offline
Light Poster

Re: Reverse print

 
0
  #5
Jul 13th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Reverse print

 
0
  #6
Jul 13th, 2007
>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.
Last edited by Narue; Jul 13th, 2007 at 10:46 am.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2498 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC