---
title: "Convert case based on first character"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:swap-case-first-char-word"
source: https://www.bctengineer.com/q/semester_1/computer_programming/swap-case-first-char-word
site: BCT Engineering
---

# Convert case based on first character

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2072 Kartik, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/swap-case-first-char-word
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program to read a word from a main function, pass it into a function that will convert all of its characters into upper case if the first character is in lower case and into lower case if the first character is in upper case. Display the converted string from main function.

## Short answer

The function checks first character: if islower(s[0]), all characters converted with toupper(); else with tolower(). The word is read in main(), modified in place, and displayed.

## Answer

**Logic.** `convert()` first checks the first character: `islower(s[0])`. If it is lower case, every character is changed with `toupper()`, otherwise with `tolower()`. The word is read in `main()`, converted in the function (the array is passed by address) and displayed in `main()`.

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

void convert(char s[])
{
    int toUpper = islower((unsigned char)s[0]);   /* decided by first character */
    for (int i = 0; s[i] != '\0'; i++)
        s[i] = toUpper ? toupper((unsigned char)s[i]) : tolower((unsigned char)s[i]);
}

int main(void)
{
    char w[50];
    printf("Enter a word: ");
    scanf("%49s", w);
    convert(w);
    printf("Converted: %s\n", w);
    return 0;
}
```

**Sample run**

Input: `hELlo`

```text
Enter a word: Converted: HELLO
```

Because the first letter `h` is lower case, the whole word becomes upper case. For an input such as `Hello` the result would be `hello`.

## Check yourself

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

1. What decides the conversion direction?
   A. The length of the word
   B. The case of the last character
   C. The number of vowels
   D. The case of the first character
   Answer: D. islower(s[0]) selects toupper for the whole word. (Key point: Decision)

2. If the word is Hello, what is the result?
   A. HELLO
   B. hello
   C. Hello
   D. hELLO
   Answer: B. The first letter is upper case, so every character becomes lower case. (Key point: Rule)

3. If the input is hELlo, the output is
   A. hello
   B. Hello
   C. HeLLO
   D. HELLO
   Answer: D. First letter h is lower, so all letters become upper case. (Key point: Rule 2)

## Related in Computer Programming

- [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×
- [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
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3

## Related in other subjects

- [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: display only alphabetic characters](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alphabetic-only) — Microprocessor, chapter 3
- [8086 ALP: alternate case display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-alternate-case) — Microprocessor, chapter 3
- [8085 ALP: add upper and lower nibbles](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-nibbles) — Microprocessor, chapter 2
