-4

I want to reverse each word of every string this program works fine for small number of inputs. But when I try to input many lines the programs its occurs runtime error for Example: if "I input hate you." output will be "I tupni etah .uoy"

 #include<stdio.h>

    int main(){
    //    freopen("output.txt","w",stdout);
        char string[256];
        int i,j,word_start;
        while(gets(string)){
        for(word_start=0,i=0;1;i++){
            if(string[i]==' '|| string[i]=='\0'){
            for(j=i-1;j>=word_start;j--)
                putchar(string[j]);
            putchar(' ');
            word_start=i+1;
            if(string[i]=='\0')
                break;
            }
        }
        putchar('\n');
        }
        return 0;
    }

Compiler C++11ISO

Sample Inputs:

Assure polite his really and others figure though. Day age advantages end sufficient eat expression travelling. Of on am father by agreed supply rather either. Own handsome delicate its property mistress her end appetite. Mean are sons too sold nor said. Son share three men power boy you. Now merits wonder effect garret own. 

Attention he extremity unwilling on otherwise. Conviction up partiality as delightful is discovered. Yet jennings resolved disposed exertion you off. Left did fond drew fat head poor. So if he into shot half many long. China fully him every fat was world grave. 
This is the problem!!!
My email is a@aa@aaa.com@
every dot is a . but not all dots are .s
is it a good test case ?
aa.bb.aa.
a.....aaaa..a..a.a..a
''.'sd.f'df'.df.'f.'
...adf....fds....sdf
.,,;;,',[,;,

sdfklj ,s,df, /////sdfdf
123456 aasdf
     568  sdf a1b2c3 :)
i am r2d2.
adfsaf 4-34549230 3
adffsflkdajflkdsajflafda afdsf 9024334242342342
,.z,.czxvkjwoijeoinlkf 2890ilakjnnc
dsakff;kdsafdafsaf adfaf

Sample Output should be

erussA etilop sih yllaer dna srehto erugif .hguoht yaD ega segatnavda dne tneiciffus tae noisserpxe .gnillevart fO no ma rehtaf yb deerga ylppus rehtar .rehtie nwO emosdnah etaciled sti ytreporp ssertsim reh dne .etiteppa naeM era snos oot dlos ron .dias noS erahs eerht nem rewop yob .uoy woN stirem rednow tceffe terrag .nwo 

noitnettA eh ytimertxe gnilliwnu no .esiwrehto noitcivnoC pu ytilaitrap sa lufthgiled si .derevocsid teY sgninnej devloser desopsid noitrexe uoy .ffo tfeL did dnof werd taf daeh .roop oS fi eh otni tohs flah ynam .gnol anihC ylluf mih yreve taf saw dlrow .evarg 
sihT si eht !!!melborp
yM liame si @moc.aaa@aa@a
yreve tod si a . tub ton lla stod era s.
si ti a doog tset esac ?
.aa.bb.aa
a..a.a..a..aaaa.....a
'.f'.fd.'fd'f.ds'.''
fds....sdf....fda...
,;,[,',;;,,.

jlkfds ,fd,s, fdfds/////
654321 fdsaa
     865  fds 3c2b1a ):
i ma .2d2r
fasfda 03294543-4 3
adfalfjasdklfjadklfsffda fsdfa 2432432424334209
fklnioejiowjkvxzc.,z., cnnjkali0982
fasfadfasdk;ffkasd fafda
princebillyGK
  • 2,917
  • 1
  • 26
  • 20
  • 2
    What does `not work properly` mean? – doctorlove Dec 01 '17 at 08:18
  • 5
    **Never use `gets`**; it is obsolete and dangerous (removed from [C11](https://en.wikipedia.org/wiki/C11_(C_standard_revision))). Consider [getline](http://man7.org/linux/man-pages/man3/getline.3.html) or else [fgets](http://en.cppreference.com/w/c/io/fgets) – Basile Starynkevitch Dec 01 '17 at 08:21
  • 1
    You state "Compiler C++11", but the question is tagged as "C". Answers will be different for C++ and C; please clarify. – Stephan Lechner Dec 01 '17 at 08:24
  • Once you use a better routine, compile your code with all warnings and debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)), improve it to get no warnings, and **use the debugger** `gdb`. BTW your *fix-my-code* question is **off-topic** (and lacks some [MCVE]). – Basile Starynkevitch Dec 01 '17 at 08:24
  • 1
    Did it occur to you to read the man page for `gets` *before* using it? Did you miss the part that says "never use gets"? It is deprecated, and unsafe to use. – Tom Karzes Dec 01 '17 at 08:25
  • 1
    Possible duplicate of [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Tom Karzes Dec 01 '17 at 08:26

1 Answers1

3

Besides the fact that gets is deprecated, some lines of your sample input exceed the 256 you reserved for the input buffer. The first line of your sample input, for example, contains 327 characters. So you will yield undefined behaviour, e.g. a segfault but maybe something less obvious. I'd suggest to use fgets, like fgets(string, 256, stdin), You can also write fgets(string, sizeof(string), stdin) if string is defined as char string[256] as in your case. Note, however, that sizeof(string) would not work as intended if you defined string as type char *, e.g. when writing char *string = malloc(256).

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58