| | |
Reverse print
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
#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*
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.
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.
>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.
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
![]() |
Similar Threads
- print words in reverse in a given sentence (C)
- string help (C++)
- Sorting lists by word length: How do I do it? (Python)
Other Threads in the C Forum
- Previous Thread: populating an array using a for loop
- Next Thread: Pausing program with getchar();
Views: 2498 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






