Thread: Simple question about word reversing program

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    Simple question about word reversing program

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	char s[40], gecici;
    	int  i, n;
    	
    	
    printf("write a word : ");
    	scanf("%s",s);
    	
    	for(n=0; s[n] != '\0'; n++);
    	
    	for(i=0; i<n/2; i++){        /*I didn't understand why we                         
    
                                             have to put "n/2" as the         
    
                                                       condition */
    		gecici   = s[n-i-1];
    		s[n-i-1] = s[i];
    		s[i]     = gecici;
    	}
    	
    printf("reverse         : %s\n",s);
    	
    return0;
    }
    this is not my code it works fine and everything I am just trying to understand the logic of the code. I have no idea why we put "n/2" as the condition. if I put "n-1" it still works too. I got everything else though.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by amadeus111 View Post
    [CODE] if I put "n-1" it still works too.
    Input GeorgeSamarasDIT

    You need n/2, because you have to go until the middle!
    • you go along the word
    • pick first letter and swap it with last letter
    • Now the last is first and the first is last
    • pick 2nd letter and swap it with prelast letter
    • Now the prelast is second and the second is prelast
    • ....


    Think about it. Then scroll down.
    .
    .
    .
    .
    ..
    .
    .
    .
    You have to go until the middle of the word

    //welcome to the forum!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Thank you! It makes sense now!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    for(n=0; s[n] != '\0'; n++);
    Most people use a nice little routine called "strlen()" it results in much more readable code. When I first glanced at this code I said "this loop does absolutely nothing...". The highest priority when writing code is READABILITY.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should practise more if you saw that with first glance.

    Also, for a beginner it just ok to use it like this.

    He probably haven't use string.h yet!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing word in a file
    By code_lover in forum C Programming
    Replies: 12
    Last Post: 09-23-2011, 10:26 PM
  2. Replies: 3
    Last Post: 02-21-2011, 10:21 PM
  3. simple question asking program
    By Barrot in forum C++ Programming
    Replies: 5
    Last Post: 08-17-2005, 06:42 PM
  4. Simple Question: How do yo uend a C program?
    By S. Omnipotence in forum C Programming
    Replies: 7
    Last Post: 01-16-2002, 07:29 AM
  5. simple program question...
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2001, 07:34 PM