---
title: "Rewrite string characters alphabetically"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:string-alphabetical-order"
source: https://www.bctengineer.com/q/semester_1/computer_programming/string-alphabetical-order
site: BCT Engineering
---

# Rewrite string characters alphabetically

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2071 Shrawan, 4 marks
- Video: [Sort a string in Alphabetical order | #12 Coding Bytes | FACE Prep](https://www.youtube.com/watch?v=WRInTbo71M8) — FACE Prep
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/string-alphabetical-order
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program to read a string and rewrite its characters in alphabetical order.

## Short answer

Compare each character with later ones; if a later character has a smaller ASCII value, swap them. After sorting all pairs, the characters appear in ascending alphabetical order.

## Answer

**Logic.** Every character of the string is compared with the characters after it (a simple exchange sort). Whenever a later character has a smaller ASCII value, the two are swapped. After the loops the characters are in ascending order. For mixed upper and lower case letters, convert the string to one case first (`tolower`), because capital letters have smaller ASCII codes than small letters.

```c
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[100], t;
    printf("Enter a string: ");
    scanf("%99s", s);
    int n = strlen(s);

    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
            if (s[i] > s[j]) {             /* swap to keep smaller first */
                t = s[i];
                s[i] = s[j];
                s[j] = t;
            }
    printf("Characters in alphabetical order: %s\n", s);
    return 0;
}
```

**Sample run**

Input: `computer`

```text
Enter a string: Characters in alphabetical order: cemoprtu
```

## Check yourself

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

1. In the sorting loops, when is a swap made for s[i] and s[j] (j after i)?
   A. When s[i] > s[j], so the smaller character moves to position i
   B. When s[i] < s[j]
   C. Only when s[i] == s[j]
   D. Always, for every pair
   Answer: A. The smaller ASCII value must be kept first, so a swap occurs when the earlier character is larger. (Key point: Exchange sort logic)

2. Why does the inner loop start at j = i + 1?
   A. To skip the first character of the string forever
   B. To compare every character with itself
   C. Each character is compared only with the characters after it
   D. Because arrays start at index 1
   Answer: C. Pairs are compared with the later characters only; comparing with itself or earlier ones is unnecessary. (Key point: Inner loop range)

3. Why should a mixed-case string be converted with tolower() before sorting?
   A. Small letters have smaller ASCII codes than capitals
   B. Capital letters have smaller ASCII codes than small letters, so they would all come first
   C. scanf cannot read capital letters
   D. strlen fails on capital letters
   Answer: B. Sorting is by ASCII value, and 'Z' (90) is less than 'a' (97), so case must be unified to get true alphabetical order. (Key point: Case handling)

4. For input 'computer', what does the program print?
   A. computer
   B. cemoprut
   C. utrpomec
   D. cemoprtu
   Answer: D. Sorting c,o,m,p,u,t,e,r in ascending ASCII order gives c e m o p r t u. (Key point: Sample result)

## Related in Computer Programming

- [Sort names and addresses alphabetically](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-names-alphabetical) — Computer Programming, chapter 8
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×
- [Display characters between given ranges](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acharacters-between-ranges) — Computer Programming, chapter 5
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×
- [Check string consists of alphabet only](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-alphabet-check) — Computer Programming, chapter 7

## Related in other subjects

- [8086 ALP: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [Program for RK-4 first order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-first-order) — Numerical Method, chapter 6
- [Program for RK-4 second order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-second-order) — Numerical Method, chapter 6
- [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
