Reversing each word in a sentence

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












Below code reverse a word given by user input example.



input : This is code Review



output : siht si edoc weiveR



steps



step 1: check for whitespace if found set the start and end position and
pass to reverseword



step 2: check for null terminator and set start and end position pass to reverseword



#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)

char temp;
int i;
while(end > start)

temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;



int main()

char input[MAX];
scanf("%[^n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)

if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found

end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;

if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world

end =pos;
reverseword(start,end,input);









share|improve this question



























    up vote
    3
    down vote

    favorite












    Below code reverse a word given by user input example.



    input : This is code Review



    output : siht si edoc weiveR



    steps



    step 1: check for whitespace if found set the start and end position and
    pass to reverseword



    step 2: check for null terminator and set start and end position pass to reverseword



    #include<stdio.h>
    #include <ctype.h>
    #define MAX 100
    #define SPACE 1
    #define NEXT 1
    #define MIN_CHAR 1
    void reverseword(int start,int end,char *input)

    char temp;
    int i;
    while(end > start)

    temp=input[start];
    input[start]=input[end];
    input[end]= temp;
    start++;
    end--;



    int main()

    char input[MAX];
    scanf("%[^n]",input);
    int pos;
    int start=0,end=0;
    for(pos=0 ; input[pos] ; ++pos)

    if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found

    end = pos-SPACE;
    reverseword(start,end,input);
    start= pos+SPACE;

    if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world

    end =pos;
    reverseword(start,end,input);









    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Below code reverse a word given by user input example.



      input : This is code Review



      output : siht si edoc weiveR



      steps



      step 1: check for whitespace if found set the start and end position and
      pass to reverseword



      step 2: check for null terminator and set start and end position pass to reverseword



      #include<stdio.h>
      #include <ctype.h>
      #define MAX 100
      #define SPACE 1
      #define NEXT 1
      #define MIN_CHAR 1
      void reverseword(int start,int end,char *input)

      char temp;
      int i;
      while(end > start)

      temp=input[start];
      input[start]=input[end];
      input[end]= temp;
      start++;
      end--;



      int main()

      char input[MAX];
      scanf("%[^n]",input);
      int pos;
      int start=0,end=0;
      for(pos=0 ; input[pos] ; ++pos)

      if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found

      end = pos-SPACE;
      reverseword(start,end,input);
      start= pos+SPACE;

      if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world

      end =pos;
      reverseword(start,end,input);









      share|improve this question













      Below code reverse a word given by user input example.



      input : This is code Review



      output : siht si edoc weiveR



      steps



      step 1: check for whitespace if found set the start and end position and
      pass to reverseword



      step 2: check for null terminator and set start and end position pass to reverseword



      #include<stdio.h>
      #include <ctype.h>
      #define MAX 100
      #define SPACE 1
      #define NEXT 1
      #define MIN_CHAR 1
      void reverseword(int start,int end,char *input)

      char temp;
      int i;
      while(end > start)

      temp=input[start];
      input[start]=input[end];
      input[end]= temp;
      start++;
      end--;



      int main()

      char input[MAX];
      scanf("%[^n]",input);
      int pos;
      int start=0,end=0;
      for(pos=0 ; input[pos] ; ++pos)

      if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found

      end = pos-SPACE;
      reverseword(start,end,input);
      start= pos+SPACE;

      if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world

      end =pos;
      reverseword(start,end,input);











      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 4 at 14:49









      200_success

      124k14143401




      124k14143401









      asked Jan 4 at 11:47









      leuage

      376




      376




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Enable warnings



          The -Wall on GCC leads to:



          reverse.c:10:13: warning: unused variable ‘i’ [-Wunused-variable]


          Do not repeat yourself



          You check pos > MIN_CHAR in two places. This could be done only once:



           if (pos > MIN_CHAR)

          if(isspace(input[pos])) // case when space is found

          end = pos-SPACE;
          reverseword(start,end,input);
          start= pos+SPACE;

          if(input[pos+NEXT]=='') // case for last world

          end =pos;
          reverseword(start,end,input);




          Be consistent in your code style



          The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.



          Declare variables in smallest possible scope



          I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp could be declared inside the loop.



          Getting rid of the your #define-d values



          All your values (except for MAX) could be removed because the link between the name and the value is hard to understand at first.:w



          So far, I've got:



          #include<stdio.h>
          #include <ctype.h>
          #include <string.h>
          #define MAX 100

          void reverseword(int start, int end, char *input)

          while (end > start)

          char temp = input[start];
          input[start] = input[end];
          input[end] = temp;
          start++;
          end--;



          int main()

          // Updated for testing purposes
          // char input[MAX];
          // scanf("%[^n]",input);
          char * original = "this is code review";
          char * input = strdup(original);
          printf("%sn", input);
          int pos;
          int start = 0;
          for (pos = 0 ; input[pos] ; ++pos)

          if (pos > 1)

          if (isspace(input[pos])) // case when space is found

          int end = pos-1;
          reverseword(start, end, input);
          start = pos + 1;

          if (input[pos+1] == '') // case for last world

          int end = pos;
          reverseword(start, end, input);



          printf("%sn", input);



          Typo: You meant "word", not "world"



          Not much to add here.



          Handle the last word at the end



          You can handle the final case the end. Then you can merge the condition check togetherin the loop.



          Bug found



          We have the following behaviors:



          • "a this is code review" -> "siht a si edoc weiver"

          • " a this is code review" -> "a siht si edoc weiver"

          I have to go, I'll try to continue asap.






          share|improve this answer























          • Why don't you use a headline (# example) instead of bold text?
            – Zeta
            Jan 4 at 23:53











          • Oops, no specific reason :-(
            – Josay
            Jan 5 at 9:06

















          up vote
          2
          down vote













          Various problems exists with scanf("%[^n]",input);



          1. It can over-fill leading to UB.


          2. If the first character is a 'n', input remains un-assigned leading to UB and 'n' still in stdin.


          3. A rare input error is not detected leading to subsequent UB.


          4. On typical input, 'n' remains in stdin, which could make a difference with a larger program.


          Use fgets()



          char input[MAX + 1];
          if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
          input[strcspn(input, "n")] = ''; // lop off potential n



          When ch < 0 is....(ch) is undefined behavior. Best to make unsigned char.



          // if(isspace(input[pos]) && ....
          if(isspace((unsigned char) input[pos]) && ....



          With the context of OP's code with small strings, reverseword() using int is not a problem. For maximal functionality, C best uses size_t for array indexing and so should reverseword(). Recommend to define variables closer to where they are needed. int i; is superfluous.



          void reverseword(size_t start, size_t end, char *input) 
          while(end > start)
          char temp = input[start];
          input[start] = input[end];
          input[end] = temp;
          start++;
          end--;





          Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184273%2freversing-each-word-in-a-sentence%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            Enable warnings



            The -Wall on GCC leads to:



            reverse.c:10:13: warning: unused variable ‘i’ [-Wunused-variable]


            Do not repeat yourself



            You check pos > MIN_CHAR in two places. This could be done only once:



             if (pos > MIN_CHAR)

            if(isspace(input[pos])) // case when space is found

            end = pos-SPACE;
            reverseword(start,end,input);
            start= pos+SPACE;

            if(input[pos+NEXT]=='') // case for last world

            end =pos;
            reverseword(start,end,input);




            Be consistent in your code style



            The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.



            Declare variables in smallest possible scope



            I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp could be declared inside the loop.



            Getting rid of the your #define-d values



            All your values (except for MAX) could be removed because the link between the name and the value is hard to understand at first.:w



            So far, I've got:



            #include<stdio.h>
            #include <ctype.h>
            #include <string.h>
            #define MAX 100

            void reverseword(int start, int end, char *input)

            while (end > start)

            char temp = input[start];
            input[start] = input[end];
            input[end] = temp;
            start++;
            end--;



            int main()

            // Updated for testing purposes
            // char input[MAX];
            // scanf("%[^n]",input);
            char * original = "this is code review";
            char * input = strdup(original);
            printf("%sn", input);
            int pos;
            int start = 0;
            for (pos = 0 ; input[pos] ; ++pos)

            if (pos > 1)

            if (isspace(input[pos])) // case when space is found

            int end = pos-1;
            reverseword(start, end, input);
            start = pos + 1;

            if (input[pos+1] == '') // case for last world

            int end = pos;
            reverseword(start, end, input);



            printf("%sn", input);



            Typo: You meant "word", not "world"



            Not much to add here.



            Handle the last word at the end



            You can handle the final case the end. Then you can merge the condition check togetherin the loop.



            Bug found



            We have the following behaviors:



            • "a this is code review" -> "siht a si edoc weiver"

            • " a this is code review" -> "a siht si edoc weiver"

            I have to go, I'll try to continue asap.






            share|improve this answer























            • Why don't you use a headline (# example) instead of bold text?
              – Zeta
              Jan 4 at 23:53











            • Oops, no specific reason :-(
              – Josay
              Jan 5 at 9:06














            up vote
            4
            down vote



            accepted










            Enable warnings



            The -Wall on GCC leads to:



            reverse.c:10:13: warning: unused variable ‘i’ [-Wunused-variable]


            Do not repeat yourself



            You check pos > MIN_CHAR in two places. This could be done only once:



             if (pos > MIN_CHAR)

            if(isspace(input[pos])) // case when space is found

            end = pos-SPACE;
            reverseword(start,end,input);
            start= pos+SPACE;

            if(input[pos+NEXT]=='') // case for last world

            end =pos;
            reverseword(start,end,input);




            Be consistent in your code style



            The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.



            Declare variables in smallest possible scope



            I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp could be declared inside the loop.



            Getting rid of the your #define-d values



            All your values (except for MAX) could be removed because the link between the name and the value is hard to understand at first.:w



            So far, I've got:



            #include<stdio.h>
            #include <ctype.h>
            #include <string.h>
            #define MAX 100

            void reverseword(int start, int end, char *input)

            while (end > start)

            char temp = input[start];
            input[start] = input[end];
            input[end] = temp;
            start++;
            end--;



            int main()

            // Updated for testing purposes
            // char input[MAX];
            // scanf("%[^n]",input);
            char * original = "this is code review";
            char * input = strdup(original);
            printf("%sn", input);
            int pos;
            int start = 0;
            for (pos = 0 ; input[pos] ; ++pos)

            if (pos > 1)

            if (isspace(input[pos])) // case when space is found

            int end = pos-1;
            reverseword(start, end, input);
            start = pos + 1;

            if (input[pos+1] == '') // case for last world

            int end = pos;
            reverseword(start, end, input);



            printf("%sn", input);



            Typo: You meant "word", not "world"



            Not much to add here.



            Handle the last word at the end



            You can handle the final case the end. Then you can merge the condition check togetherin the loop.



            Bug found



            We have the following behaviors:



            • "a this is code review" -> "siht a si edoc weiver"

            • " a this is code review" -> "a siht si edoc weiver"

            I have to go, I'll try to continue asap.






            share|improve this answer























            • Why don't you use a headline (# example) instead of bold text?
              – Zeta
              Jan 4 at 23:53











            • Oops, no specific reason :-(
              – Josay
              Jan 5 at 9:06












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            Enable warnings



            The -Wall on GCC leads to:



            reverse.c:10:13: warning: unused variable ‘i’ [-Wunused-variable]


            Do not repeat yourself



            You check pos > MIN_CHAR in two places. This could be done only once:



             if (pos > MIN_CHAR)

            if(isspace(input[pos])) // case when space is found

            end = pos-SPACE;
            reverseword(start,end,input);
            start= pos+SPACE;

            if(input[pos+NEXT]=='') // case for last world

            end =pos;
            reverseword(start,end,input);




            Be consistent in your code style



            The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.



            Declare variables in smallest possible scope



            I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp could be declared inside the loop.



            Getting rid of the your #define-d values



            All your values (except for MAX) could be removed because the link between the name and the value is hard to understand at first.:w



            So far, I've got:



            #include<stdio.h>
            #include <ctype.h>
            #include <string.h>
            #define MAX 100

            void reverseword(int start, int end, char *input)

            while (end > start)

            char temp = input[start];
            input[start] = input[end];
            input[end] = temp;
            start++;
            end--;



            int main()

            // Updated for testing purposes
            // char input[MAX];
            // scanf("%[^n]",input);
            char * original = "this is code review";
            char * input = strdup(original);
            printf("%sn", input);
            int pos;
            int start = 0;
            for (pos = 0 ; input[pos] ; ++pos)

            if (pos > 1)

            if (isspace(input[pos])) // case when space is found

            int end = pos-1;
            reverseword(start, end, input);
            start = pos + 1;

            if (input[pos+1] == '') // case for last world

            int end = pos;
            reverseword(start, end, input);



            printf("%sn", input);



            Typo: You meant "word", not "world"



            Not much to add here.



            Handle the last word at the end



            You can handle the final case the end. Then you can merge the condition check togetherin the loop.



            Bug found



            We have the following behaviors:



            • "a this is code review" -> "siht a si edoc weiver"

            • " a this is code review" -> "a siht si edoc weiver"

            I have to go, I'll try to continue asap.






            share|improve this answer















            Enable warnings



            The -Wall on GCC leads to:



            reverse.c:10:13: warning: unused variable ‘i’ [-Wunused-variable]


            Do not repeat yourself



            You check pos > MIN_CHAR in two places. This could be done only once:



             if (pos > MIN_CHAR)

            if(isspace(input[pos])) // case when space is found

            end = pos-SPACE;
            reverseword(start,end,input);
            start= pos+SPACE;

            if(input[pos+NEXT]=='') // case for last world

            end =pos;
            reverseword(start,end,input);




            Be consistent in your code style



            The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.



            Declare variables in smallest possible scope



            I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp could be declared inside the loop.



            Getting rid of the your #define-d values



            All your values (except for MAX) could be removed because the link between the name and the value is hard to understand at first.:w



            So far, I've got:



            #include<stdio.h>
            #include <ctype.h>
            #include <string.h>
            #define MAX 100

            void reverseword(int start, int end, char *input)

            while (end > start)

            char temp = input[start];
            input[start] = input[end];
            input[end] = temp;
            start++;
            end--;



            int main()

            // Updated for testing purposes
            // char input[MAX];
            // scanf("%[^n]",input);
            char * original = "this is code review";
            char * input = strdup(original);
            printf("%sn", input);
            int pos;
            int start = 0;
            for (pos = 0 ; input[pos] ; ++pos)

            if (pos > 1)

            if (isspace(input[pos])) // case when space is found

            int end = pos-1;
            reverseword(start, end, input);
            start = pos + 1;

            if (input[pos+1] == '') // case for last world

            int end = pos;
            reverseword(start, end, input);



            printf("%sn", input);



            Typo: You meant "word", not "world"



            Not much to add here.



            Handle the last word at the end



            You can handle the final case the end. Then you can merge the condition check togetherin the loop.



            Bug found



            We have the following behaviors:



            • "a this is code review" -> "siht a si edoc weiver"

            • " a this is code review" -> "a siht si edoc weiver"

            I have to go, I'll try to continue asap.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jan 4 at 13:38


























            answered Jan 4 at 12:54









            Josay

            23.8k13580




            23.8k13580











            • Why don't you use a headline (# example) instead of bold text?
              – Zeta
              Jan 4 at 23:53











            • Oops, no specific reason :-(
              – Josay
              Jan 5 at 9:06
















            • Why don't you use a headline (# example) instead of bold text?
              – Zeta
              Jan 4 at 23:53











            • Oops, no specific reason :-(
              – Josay
              Jan 5 at 9:06















            Why don't you use a headline (# example) instead of bold text?
            – Zeta
            Jan 4 at 23:53





            Why don't you use a headline (# example) instead of bold text?
            – Zeta
            Jan 4 at 23:53













            Oops, no specific reason :-(
            – Josay
            Jan 5 at 9:06




            Oops, no specific reason :-(
            – Josay
            Jan 5 at 9:06












            up vote
            2
            down vote













            Various problems exists with scanf("%[^n]",input);



            1. It can over-fill leading to UB.


            2. If the first character is a 'n', input remains un-assigned leading to UB and 'n' still in stdin.


            3. A rare input error is not detected leading to subsequent UB.


            4. On typical input, 'n' remains in stdin, which could make a difference with a larger program.


            Use fgets()



            char input[MAX + 1];
            if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
            input[strcspn(input, "n")] = ''; // lop off potential n



            When ch < 0 is....(ch) is undefined behavior. Best to make unsigned char.



            // if(isspace(input[pos]) && ....
            if(isspace((unsigned char) input[pos]) && ....



            With the context of OP's code with small strings, reverseword() using int is not a problem. For maximal functionality, C best uses size_t for array indexing and so should reverseword(). Recommend to define variables closer to where they are needed. int i; is superfluous.



            void reverseword(size_t start, size_t end, char *input) 
            while(end > start)
            char temp = input[start];
            input[start] = input[end];
            input[end] = temp;
            start++;
            end--;





            Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.






            share|improve this answer



























              up vote
              2
              down vote













              Various problems exists with scanf("%[^n]",input);



              1. It can over-fill leading to UB.


              2. If the first character is a 'n', input remains un-assigned leading to UB and 'n' still in stdin.


              3. A rare input error is not detected leading to subsequent UB.


              4. On typical input, 'n' remains in stdin, which could make a difference with a larger program.


              Use fgets()



              char input[MAX + 1];
              if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
              input[strcspn(input, "n")] = ''; // lop off potential n



              When ch < 0 is....(ch) is undefined behavior. Best to make unsigned char.



              // if(isspace(input[pos]) && ....
              if(isspace((unsigned char) input[pos]) && ....



              With the context of OP's code with small strings, reverseword() using int is not a problem. For maximal functionality, C best uses size_t for array indexing and so should reverseword(). Recommend to define variables closer to where they are needed. int i; is superfluous.



              void reverseword(size_t start, size_t end, char *input) 
              while(end > start)
              char temp = input[start];
              input[start] = input[end];
              input[end] = temp;
              start++;
              end--;





              Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                Various problems exists with scanf("%[^n]",input);



                1. It can over-fill leading to UB.


                2. If the first character is a 'n', input remains un-assigned leading to UB and 'n' still in stdin.


                3. A rare input error is not detected leading to subsequent UB.


                4. On typical input, 'n' remains in stdin, which could make a difference with a larger program.


                Use fgets()



                char input[MAX + 1];
                if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
                input[strcspn(input, "n")] = ''; // lop off potential n



                When ch < 0 is....(ch) is undefined behavior. Best to make unsigned char.



                // if(isspace(input[pos]) && ....
                if(isspace((unsigned char) input[pos]) && ....



                With the context of OP's code with small strings, reverseword() using int is not a problem. For maximal functionality, C best uses size_t for array indexing and so should reverseword(). Recommend to define variables closer to where they are needed. int i; is superfluous.



                void reverseword(size_t start, size_t end, char *input) 
                while(end > start)
                char temp = input[start];
                input[start] = input[end];
                input[end] = temp;
                start++;
                end--;





                Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.






                share|improve this answer















                Various problems exists with scanf("%[^n]",input);



                1. It can over-fill leading to UB.


                2. If the first character is a 'n', input remains un-assigned leading to UB and 'n' still in stdin.


                3. A rare input error is not detected leading to subsequent UB.


                4. On typical input, 'n' remains in stdin, which could make a difference with a larger program.


                Use fgets()



                char input[MAX + 1];
                if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
                input[strcspn(input, "n")] = ''; // lop off potential n



                When ch < 0 is....(ch) is undefined behavior. Best to make unsigned char.



                // if(isspace(input[pos]) && ....
                if(isspace((unsigned char) input[pos]) && ....



                With the context of OP's code with small strings, reverseword() using int is not a problem. For maximal functionality, C best uses size_t for array indexing and so should reverseword(). Recommend to define variables closer to where they are needed. int i; is superfluous.



                void reverseword(size_t start, size_t end, char *input) 
                while(end > start)
                char temp = input[start];
                input[start] = input[end];
                input[end] = temp;
                start++;
                end--;





                Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Jan 5 at 2:15


























                answered Jan 5 at 2:06









                chux

                11.4k11238




                11.4k11238






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184273%2freversing-each-word-in-a-sentence%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation