# Simple question about word reversing program

• 01-15-2013
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.
• 01-15-2013
std10093
Quote:

[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!
• 01-15-2013
Thank you! It makes sense now! :) :)
• 01-15-2013
std10093
You are welcome :D
• 01-15-2013
nonpuz
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.
• 01-16-2013
std10093
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!