# Reversing a String

### Procedure :

* Calculate the length of string if it isn't given as input.
* Initialise i with 0 and j with length - 1.
* Run a for loop till i < j.
* Swap the i-th and j-th character in each iteration.

### C Function :

```c
void reverse(char a[], int n) {

    // Initialise i with 0 and j with length - 1
    int i = 0, j = n-1;
    char temp;

    // Swap characters in each iteration
    for (i = 0, j = n-1; i < j; i++, j--) {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    
}

```

Contributed by Nitin Ranganath


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nitinranganath.gitbook.io/data-structures/strings/reversing-a-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
