---
title: "Copy string converting lower to upper using pointer"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-array-elements-copy-string-upper"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-array-elements-copy-string-upper
site: BCT Engineering
---

# Copy string converting lower to upper using pointer

Computer Programming · Chapter 9: Pointers

- Asked in: 2070 Asar, 5 marks; 2069 Chaitra, 5 marks; 2068 Chaitra, 1+1+6 marks (asked 3 times)
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-array-elements-copy-string-upper
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

Write a program that reads a string from user and use a user defined function to copy the content of the read string into another character array changing lower case letter to upper if any. Use pointer to process the string.

## Short answer

copyUpper(char *src, char *dest) uses pointer loops: if *src between 'a'-'z', store *src-32 to dest; else copy *src. Increment both pointers, then null-terminate dest. Main reads string, removes newline, calls function, prints original and uppercase copy.

## Answer

**Logic.** The function `copyUpper(char *src, char *dest)` walks along the source string with the pointer `src`. For each character `*src`: if it lies between `'a'` and `'z'`, 32 is subtracted (the ASCII code of a small letter is 32 more than that of the capital letter) and stored through `dest`; otherwise it is copied unchanged. Both pointers are incremented after every character, and `'\0'` is added at the end of `dest`. No array subscripts are used.

```c
#include <stdio.h>

/* copy src to dest, converting lower case letters to upper case */
void copyUpper(char *src, char *dest)
{
    while (*src != '\0') {
        if (*src >= 'a' && *src <= 'z')
            *dest = *src - 32;        /* 'a'(97) - 32 = 'A'(65) */
        else
            *dest = *src;
        src++;
        dest++;
    }
    *dest = '\0';
}

int main(void)
{
    char s1[100], s2[100];
    printf("Enter a string: ");
    fgets(s1, sizeof s1, stdin);
    for (char *p = s1; *p; p++)
        if (*p == '\n') *p = '\0';
    copyUpper(s1, s2);
    printf("Original: %s\nCopied  : %s\n", s1, s2);
    return 0;
}
```

**Sample run**

Input: `Hello World 2024 from Nepal`

```text
Enter a string: Original: Hello World 2024 from Nepal
Copied  : HELLO WORLD 2024 FROM NEPAL
```

Digits and spaces are unchanged; only the lower case letters are converted.

## Check yourself

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

1. In copyUpper, how is a lowercase letter converted to uppercase?
   A. 32 is added to its ASCII code
   B. 32 is subtracted from its ASCII code
   C. It is multiplied by 32
   D. It is XORed with 0
   Answer: B. The ASCII code of a small letter is 32 more than the capital, so subtracting 32 gives the capital (97 - 32 = 65). (Key point: Lowercase to uppercase conversion)

2. Which condition decides that a character should be converted?
   A. *src >= 'a' && *src <= 'z'
   B. *src >= 'A' && *src <= 'Z'
   C. *src != '\0'
   D. *src >= '0' && *src <= '9'
   Answer: A. Only characters between 'a' and 'z' are lowercase letters; digits, spaces and capitals are copied unchanged. (Key point: Range test)

3. Why must the function write '\0' at the end of dest after the loop?
   A. To convert the last letter
   B. So that dest becomes a valid string
   C. To increment the pointers
   D. To free the memory
   Answer: B. The loop stops before copying the terminator, so it must be added for dest to be a proper string. (Key point: Terminating the copy)

4. How does the function move through the strings without subscripts?
   A. It uses scanf for each character
   B. It increments src and dest after each character
   C. It resets the pointers each time
   D. It copies the entire array with one assignment
   Answer: B. src++ and dest++ after every character walk both pointers along the strings. (Key point: Pointer processing)

## Related in Computer Programming

- [Convert case based on first character](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswap-case-first-char-word) — Computer Programming, chapter 7
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [Insert a string into another string](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ainsert-string-into-string) — Computer Programming, chapter 7
- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [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
- [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

- [8085 ALP: add upper and lower nibbles](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-nibbles) — Microprocessor, chapter 2
- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [Multimedia user interface design process](https://www.bctengineer.com/question.md?key=semester_8%2Fmultimedia_system%3Aui-design-process) — Multimedia System, chapter 6, asked 2×
- [Process vs thread; user and kernel threads](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Aprocess-vs-thread-user-kernel-threads) — Operating System, chapter 2
