---
title: "Reverse a string using functions"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:string-reverse-with-count-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/string-reverse-with-count-function
site: BCT Engineering
---

# Reverse a string using functions

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2071 Chaitra, 4 marks; 2068 Chaitra, 8 marks (asked 2 times)
- Video: [Reverse a string without using strrev() String function||C language tutorial for beginners](https://www.youtube.com/watch?v=jxQKADifkDo) — TechAeron
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/string-reverse-with-count-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a C program to read a string and display its reverse. Use user defined function to count number of characters in it and to reverse it.

## Short answer

countChars returns the string length by counting characters until '\0'. reverse copies characters from the end of the source to the start of the destination and appends '\0'. main reads with fgets, strips the newline, and prints length and reversed string. For 'Pulchowk Campus', length=15, reverse='supmaC kwohcluP'.

## Answer

**Method.**
1. Read the string (using `fgets` so that spaces are accepted).
2. `countChars()` returns the number of characters by counting until the null character `'\0'`.
3. `reverse()` copies the characters from the last to the first into a second array and puts `'\0'` at the end.
4. `main()` displays the length and the reversed string.

```c
#include <stdio.h>

int countChars(char s[])
{
    int n = 0;
    while (s[n] != '\0')
        n++;
    return n;
}

void reverse(char s[], char r[])
{
    int n = countChars(s);
    for (int i = 0; i < n; i++)
        r[i] = s[n - 1 - i];
    r[n] = '\0';
}

int main(void)
{
    char str[100], rev[100];
    printf("Enter a string: ");
    fgets(str, sizeof str, stdin);
    for (int i = 0; str[i]; i++)            /* remove trailing newline */
        if (str[i] == '\n') str[i] = '\0';
    reverse(str, rev);
    printf("Length = %d\nReverse = %s\n", countChars(str), rev);
    return 0;
}
```

**Sample run**

Input: `Pulchowk Campus`

```text
Enter a string: Length = 15
Reverse = supmaC kwohcluP
```

The length of "Pulchowk Campus" is 15 (the space is counted).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is fgets used instead of scanf("%s")?
   A. So that strings containing spaces are accepted
   B. Because fgets is faster
   C. Because scanf cannot read characters
   D. Because fgets removes the null character
   Answer: A. scanf with %s stops at whitespace, while fgets reads the entire line. (Key point: Reading with spaces)

2. How does countChars find the length?
   A. It calls sizeof on the array
   B. It counts until a space appears
   C. It counts characters until the null character '\0'
   D. It counts until the newline is found in the array size
   Answer: C. Strings end with '\0', so counting stops there; it is not included in the length. (Key point: Counting characters)

3. How does reverse() build the reversed string?
   A. r[i] = s[n-1-i] for i = 0 to n-1, then r[n] = '\0'
   B. r[i] = s[i] for i = n-1 down to 0
   C. r[i] = s[n-i] with no terminator
   D. It swaps s[0] with s[n] only
   Answer: A. The last character goes first; the null terminator must be added at the end of the new array. (Key point: Reversing)

4. For "Pulchowk Campus", what length is displayed?
   A. 14
   B. 16
   C. 15
   D. 13
   Answer: C. 8 letters + 1 space + 6 letters = 15; the space is counted. (Key point: Length of sample)

## Related in Computer Programming

- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Rewrite string characters alphabetically](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabetical-order) — Computer Programming, chapter 7
- [Display characters between given ranges](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acharacters-between-ranges) — Computer Programming, chapter 5
- [Palindrome string using function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apalindrome-string-function) — Computer Programming, chapter 7
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9

## Related in other subjects

- [8086 ALP: count vowels and display string](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-display) — Microprocessor, chapter 3
- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [8086 ALP: count vowels, consonants, numerals](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-count-vowels-consonants-numerals) — Microprocessor, chapter 3
