Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C program to reverse the order of the words entered.

C program to reverse the order of the words entered.

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.

    #include
    #include

    #define EOL '\n'
    #define MAX_SIZE 100

    int main(){

    char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\];
    int count, i, mark\[50\];
    
    //reading the text
    for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){
        mark\[count\]=0;
    }
    text\[count\] = '\\0';
    
    //reversing the text
    for(i = count-1; i>=0; --i){
        back\[(count-1)-i\] = text\[i\];
    }
    back\[count\] = '\\0';
    
    //noting the frequencies of the occurences of space
    for(i=count-1; i>=0; --i){
    	if(back\[i\]== ' '){
            mark\[i\] = i;
        }
    }
    
    //irr-reversing each words at their respective place
    int j, k=0, l=0;
    for(i=0; i\=k; j--){
                temp\[l\] = back\[j\];					//it reverse the words btw two space entries
                l++;
            }           
            temp\[mark\[i\]\] = ' ';
            l= l+1;
            k = mark\[i\]+1;
        }
    }
    for(i=count-1; i>=k; i--){
    	temp\[l\] = back\[i\];				//it reverse the word after the last space entry. 
    	l++;
    }
    
    //printing the string.
    printf("\\n%s", temp);
    return 0;
    

    }

    Thank you.

    P D 2 Replies Last reply
    0
    • T Tarun Jha

      for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.

      #include
      #include

      #define EOL '\n'
      #define MAX_SIZE 100

      int main(){

      char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\];
      int count, i, mark\[50\];
      
      //reading the text
      for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){
          mark\[count\]=0;
      }
      text\[count\] = '\\0';
      
      //reversing the text
      for(i = count-1; i>=0; --i){
          back\[(count-1)-i\] = text\[i\];
      }
      back\[count\] = '\\0';
      
      //noting the frequencies of the occurences of space
      for(i=count-1; i>=0; --i){
      	if(back\[i\]== ' '){
              mark\[i\] = i;
          }
      }
      
      //irr-reversing each words at their respective place
      int j, k=0, l=0;
      for(i=0; i\=k; j--){
                  temp\[l\] = back\[j\];					//it reverse the words btw two space entries
                  l++;
              }           
              temp\[mark\[i\]\] = ' ';
              l= l+1;
              k = mark\[i\]+1;
          }
      }
      for(i=count-1; i>=k; i--){
      	temp\[l\] = back\[i\];				//it reverse the word after the last space entry. 
      	l++;
      }
      
      //printing the string.
      printf("\\n%s", temp);
      return 0;
      

      }

      Thank you.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I'd make a map of the words (array of pointers to char), then iterate it backward.

      1 Reply Last reply
      0
      • T Tarun Jha

        for ex, input: My name is Bay Max. output: Max Bay is name My. below is my code, but its too long and messy. How do i make it more concise and readable? Any alternative would be appreciated.

        #include
        #include

        #define EOL '\n'
        #define MAX_SIZE 100

        int main(){

        char text\[MAX\_SIZE\], back\[MAX\_SIZE\], temp\[MAX\_SIZE\];
        int count, i, mark\[50\];
        
        //reading the text
        for(count=0; (text\[count\] = getc(stdin))!= EOL; ++count){
            mark\[count\]=0;
        }
        text\[count\] = '\\0';
        
        //reversing the text
        for(i = count-1; i>=0; --i){
            back\[(count-1)-i\] = text\[i\];
        }
        back\[count\] = '\\0';
        
        //noting the frequencies of the occurences of space
        for(i=count-1; i>=0; --i){
        	if(back\[i\]== ' '){
                mark\[i\] = i;
            }
        }
        
        //irr-reversing each words at their respective place
        int j, k=0, l=0;
        for(i=0; i\=k; j--){
                    temp\[l\] = back\[j\];					//it reverse the words btw two space entries
                    l++;
                }           
                temp\[mark\[i\]\] = ' ';
                l= l+1;
                k = mark\[i\]+1;
            }
        }
        for(i=count-1; i>=k; i--){
        	temp\[l\] = back\[i\];				//it reverse the word after the last space entry. 
        	l++;
        }
        
        //printing the string.
        printf("\\n%s", temp);
        return 0;
        

        }

        Thank you.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        I would be tempted to use strtok(). Then just read the returned value in reverse order.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups